Repository navigation
Set Vite's publicDir and correctly serve public assets earlier in pipeline - #5686
Conversation
🦋 Changeset detectedLatest commit: 9cee0ab The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
|
Can we add a test to this to guard against a recurrence of #5672? |
|
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'); |
There was a problem hiding this comment.
is this actually a thing that can happen? if i remove this line and get rid of the try-catch, TypeScript doesn't complain
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Are you sure? I don't see any code there that's in danger of throwing
There was a problem hiding this comment.
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/[email protected]/node_modules/vite/dist/node/chunks/dep-561c5231.js:45966:7)
at next (node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-561c5231.js:45910:5)
at Function.handle (node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-561c5231.js:45913:3)
at Server.app (node_modules/.pnpm/[email protected]/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)
Fixes #17650 The dev asset middleware decodes every request pathname before deciding whether it names a static asset. Invalid percent escapes therefore throw before application routing and become Vite 500 error responses, including for malformed image URLs such as `/%%7BimgPath%7D`. The early asset handler introduced in #5686 remains necessary to keep public assets out of Vite's transform middleware. #12762 removed its broad catch because Vite already handles unexpected errors; restoring that catch, or copying the later handler's catch, would still produce a 500. Catch only pathname decoding failures and return a small 400 response before continuing through the middleware chain. This also prevents these invalid paths from reaching the later dev decoder. URL parsing, filesystem failures, asset serving, case checks, and base-path handling retain their existing behavior. Preview already guards decoding and falls through to the runtime's malformed-URI handling, so it needs no change. Unlike a forgiving decoder or broad middleware catch, this does not pass malformed paths downstream or misclassify genuine server errors as client errors. The behavior covers document and subresource requests while preserving valid encoded paths and percent escapes in query strings. - [Ported reproduction](https://www.sveltelab.dev/#code=N4IgzgTgxiBcoBMCWECmUAuB7CBPOoAhgA7EB0AFhgLYA2BIAZkrag1FgHYardhwgAPAEIEWTLmKoABFToA%2BADqdBc2tNqFOAcwC8ikLwNLOijKtSEEJs2cHVUGQtKgVCEMI-0gArhkYAtAAcBtIA9DYYdg5O0pyEDt4AbkioAO7EOFEgLlw83N5pSAgYFLoIqClQqAFFJRQANNJInEgYSIS0AWBQnai6AIyhEcq25jHO8YkGPAAeGN29rKEc3LwY3j19w5FmAKRgSai0PADWbZSWCHuj5mEUV5GCAEZYCLjSCIRO3UcnqOcFsQ0LQsFYAl8nN4KFgjhBjLc7MgktIwBhcKxvMgwMRNLhYLk1nxjAc-mcLq93ntBGFkU8wpTcCYaWoTCAAL7shogCBYPyofjwEDINCYHD4IUAajxfIwZEOxx4DGYrHYeXWgqEPQgSGIGA0Wj0MzACNM7WomQg%2BvRUmkwGkABlCLhZQAFXnEMDSdnSRi86jSADkZDCABIbQLAwBuUZm1j6%2B2uFgINCcb0Ep0uvzurCe6S6aSh4G5sAACgAlDGVGFtbqMDZOMAAAKpioQFwUZOpivs5QcrkgSXEQjaVDyslsIUqyegVb5DCa1QDeQAdWOHAc0mw0gAyhOANJtFnL5SCYjyABqSDAbWkgmcFDQjGhGAwntgYRrE7IFSStPEYBhICxgKv8P6VP%2BUCAYCNKEPIW5YNIaBWFuDyfOIPgONw3xIFwNLn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D` while running in dev mode. <!-- svelte-triage-bot:feedback-baseline:af9ba601bc93d4df09052edff9fb18620dd9bd85 --> --------- Co-authored-by: svelte-triage-bot <[email protected]> Co-authored-by: Tee Ming <[email protected]>
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