Skip to content

Set Vite's publicDir and correctly serve public assets earlier in pipeline#5686

Merged
Rich-Harris merged 6 commits intosveltejs:masterfrom
benmccann:publi2
Jul 25, 2022
Merged

Set Vite's publicDir and correctly serve public assets earlier in pipeline#5686
Rich-Harris merged 6 commits intosveltejs:masterfrom
benmccann:publi2

Conversation

@benmccann
Copy link
Member

This is a second stab at #5648 which was reverted for causing #5672

The issue was that we were serving static files from within the Kit middleware, so the transform middleware was being executed on them. This moves static serving into its own middleware so that they're served without the transform middleware being called on them

@changeset-bot
Copy link

changeset-bot bot commented Jul 23, 2022

🦋 Changeset detected

Latest commit: 9cee0ab

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@sveltejs/adapter-node Patch
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Copy link
Member

Can we add a test to this to guard against a recurrence of #5672?

@benmccann
Copy link
Member Author

Yes, thanks for the reminder. I had intended to do that. Done!


vite.middlewares.use(async (req, res, next) => {
try {
if (!req.url || !req.method) throw new Error('Incomplete request');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this actually a thing that can happen? if i remove this line and get rid of the try-catch, TypeScript doesn't complain

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think we need to worry about it. I had copied it from the main middleware. I've removed it in both places. The try/catch on the otherhand can't be removed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? I don't see any code there that's in danger of throwing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you try to remove it there's a test that fails with the error below showing where it can throw. It must bring down the whole server because all subsequent tests then fail.

[WebServer] file:///home/bmccann/src/kit/packages/kit/src/vite/dev/index.js:188
			const decoded = decodeURI(new URL(base + req.url).pathname);
			                ^

URIError: URI malformed
    at decodeURI (<anonymous>)
    at packages/kit/src/vite/dev/index.js:188:20
    at call (node_modules/.pnpm/vite@3.0.0/node_modules/vite/dist/node/chunks/dep-561c5231.js:45966:7)
    at next (node_modules/.pnpm/vite@3.0.0/node_modules/vite/dist/node/chunks/dep-561c5231.js:45910:5)
    at Function.handle (node_modules/.pnpm/vite@3.0.0/node_modules/vite/dist/node/chunks/dep-561c5231.js:45913:3)
    at Server.app (node_modules/.pnpm/vite@3.0.0/node_modules/vite/dist/node/chunks/dep-561c5231.js:45778:37)
    at Server.emit (node:events:527:28)
    at parserOnIncoming (node:_http_server:956:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, gotcha. fair enough

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants