Skip to content

feat: server.htmlEntryPoint config option#4640

Closed
benmccann wants to merge 1 commit intovitejs:mainfrom
benmccann:html-entry-point
Closed

feat: server.htmlEntryPoint config option#4640
benmccann wants to merge 1 commit intovitejs:mainfrom
benmccann:html-entry-point

Conversation

@benmccann
Copy link
Collaborator

@benmccann benmccann commented Aug 17, 2021

Description

Adds a server.htmlEntryPoint config option

Additional context

SvelteKit right now runs in middleware mode and has to provide its own set of server options. As a result, people get confused between SvelteKit's server options and Vite's server options. It also means a lot of duplicate functionality like https certificate handling. This change would allow SvelteKit to run a Vite server from the SvelteKit CLI. SvelteKit's middleware is then injected using a plugin with the configureServer hook.

Proof-of-concept PR is pending on the SvelteKit side that could be cleaned up and checked in after this PR is released: sveltejs/kit#2232. It will need to have server.htmlEntryPoint: false set in order to work

This idea was originally suggested by @axe-me: #4230 (comment)


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@Shinigami92 Shinigami92 added the p2-nice-to-have Not breaking anything but nice to have (priority) label Aug 18, 2021
@axe-me
Copy link
Contributor

axe-me commented Aug 19, 2021

Instead of having a specific config option to disable HTML middleware, would it be better to have an option to disable a list of middleware? This way, users could have more flexibility to config the server builtin middleware. something like:

// config
server: {
  disabledMiddlewares: ['html', 'fallback']
}
// server/index.ts
if (!serverConfig.disabledMiddlewares.includes('html')) {
  middlewares.use(indexHtmlMiddleware(server))
}

@benmccann
Copy link
Collaborator Author

benmccann commented Aug 19, 2021

Maybe not a bad idea if middlewares had names, but we'd basically be having to create a string for each ourselves used just for this purpose.

I also think it'd be harder to use. E.g. in this case you'd have to disable three different middlewares and the only way you'd know which to disable would be to open up the Vite source code vs just setting a single documented option.

Finally, it'd make merging options harder. SvelteKit defaults some Vite options and we'd want to set this one so that users don't need to set it to make their apps work. That's relatively easy with a boolean, but can get more complicated to merge user-supplied options with our defaults when the options are more complex data structures

@axe-me
Copy link
Contributor

axe-me commented Aug 20, 2021

Hi @benmccann , I read the vite server source code again today, I just realized you don't really need to worry about the HTML middleware if your middleware ends the request early.
https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/index.ts#L529 from here you can see user middleware gets handle the request before the html middleware.

I just checked your svelteKit PR, looks like your middleware does end the request in all cases, and never call the next() function. https://github.com/sveltejs/kit/pull/2232/files#diff-ed48923f0655f935bee505cdcbf40212129ff49a89c2c9d752bd63aa1e6282c4R497
Therefore, this PR prob not needed for sveltekit 1.0

@benmccann
Copy link
Collaborator Author

I think you tagged the wrong Ben 😄 But this actually is needed because the SvelteKit middleware must be added after the Vite middleware. Vite must be first because it handles the static assets. Then for requests that aren't for static assets it can fallthrough to the SvelteKit middleware. If I put SvelteKit first then no static asset would ever be served because SvelteKit never calls next() as you point out

@axe-me
Copy link
Contributor

axe-me commented Aug 20, 2021

Hi @benmccann , the static assets middleware is actually pushed to the stack before user configured middlewares from the code I saw. Please give it a try, let me know if that doesn't work for you.

@benmccann
Copy link
Collaborator Author

I did try it. It doesn't work

@axe-me
Copy link
Contributor

axe-me commented Aug 20, 2021

@benmccann sorry, my bad, looks like the spa fallback middleware should be installed after post hooks. Let me see if I can find the reason why it's before post hook.

@axe-me
Copy link
Contributor

axe-me commented Aug 20, 2021

@benmccann I just tested at my local. If I move the spa fallback middleware after the postHooks calls, everything works as expected.

I also checked the git history, seems the posthooks always run after the fallback middleware. This actually does not make sense to me, since this is also part of vite html serve logic which should go after post hooks? @yyx990803 sorry to ping you, but do you remember the reason behind this? is it OK for us to move the spa-fallback middleware after post hooks?

@benmccann
Copy link
Collaborator Author

benmccann commented Aug 20, 2021

good idea. closing in favor of #4670

@benmccann benmccann closed this Aug 20, 2021
@benmccann benmccann reopened this Aug 29, 2021
@benmccann
Copy link
Collaborator Author

that did not work (#4670 (comment)), so reopening this PR instead

@patak-cat patak-cat added p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) and removed p2-nice-to-have Not breaking anything but nice to have (priority) labels Aug 29, 2021
Shinigami92
Shinigami92 previously approved these changes Aug 30, 2021
@patak-cat
Copy link
Member

@benmccann we discussed this today. In principle, this is a valid request. But it is not yet clear if adding a single boolean option is the best here. In case it is boolean, it could be named server.serveHtml but it may be more flexible to allow disabling groups or individual middleware with something like server.middlewares: { html: false }. A review of current SSR playgrounds could help to better identify what is best, as the servers could be potentially simplified with this option.

Another option could be to identify and remove this middleware from the array. The internal middlewares are currently named function

return async function viteIndexHtmlMiddleware(req, res, next) {

Although seems like the connect one for the SPA fallback is unnamed https://github.com/bripkens/connect-history-api-fallback/blob/48e170c92c53e6f2bdb8755175bc52e0bc8b838a/lib/index.js#L9
This may not be a good option as it would expose a lot more internals of what we want. I think some integrations are already removing some middlewares in this way though.

@benmccann
Copy link
Collaborator Author

Thanks for taking a look at this @patak-js. I really appreciate you driving all these PRs to completion

The other alternative for implementing this that you mentioned is actually one we'd considered above. I tend to think it's not the best way to do it for the reasons mentioned in this comment: #4640 (comment). I'm happy to reconsider if you all have other thoughts about it

@benmccann
Copy link
Collaborator Author

Closing this since it seems to have gotten hung up and there's more important fixes to get in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants