diff --git a/.changeset/tame-lions-jump.md b/.changeset/tame-lions-jump.md new file mode 100644 index 0000000000000..94c611bdb81b0 --- /dev/null +++ b/.changeset/tame-lions-jump.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-node': patch +--- + +[fix] workaround sirv bug to allow routes with unicode characters diff --git a/packages/adapter-node/src/server.js b/packages/adapter-node/src/server.js index 0e538042935c9..4ad7284aef773 100644 --- a/packages/adapter-node/src/server.js +++ b/packages/adapter-node/src/server.js @@ -26,6 +26,17 @@ export function createServer({ render }) { }) : noop_handler; + // handle static assets with unicode characters + // e.g. a route with unicode character will generate a .js file with unicode character + // https://github.com/lukeed/sirv/issues/82#issuecomment-898618504 + const sirv_workaround_handler = fs.existsSync(paths.assets) + ? (req, _, next) => { + req._origPath = req.path; + req.path = decodeURI(req.path); + next(); + } + : noop_handler; + const assets_handler = fs.existsSync(paths.assets) ? sirv(paths.assets, { setHeaders: (res, pathname, stats) => { @@ -39,7 +50,16 @@ export function createServer({ render }) { }) : noop_handler; + const revert_sirv_workaround_handler = fs.existsSync(paths.assets) + ? (req, _, next) => { + req.path = req._origPath; + next(); + } + : noop_handler; + const server = polka(); + // workaround https://github.com/lukeed/polka/issues/142 + // until https://github.com/lukeed/polka/pull/172 is merged and released // Polka has a non-standard behavior of decoding the request path // Disable it so that adapter-node works just like the rest // SvelteKit will handle decoding URI components into req.params @@ -48,8 +68,10 @@ export function createServer({ render }) { }; server.use( compression({ threshold: 0 }), + sirv_workaround_handler, assets_handler, prerendered_handler, + revert_sirv_workaround_handler, async (req, res) => { const parsed = new URL(req.url || '', 'http://localhost');