Skip to content

Commit

Permalink
[fix] workaround sirv bug
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Aug 13, 2021
1 parent 7c1d7e6 commit 511a1dc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-lions-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

[fix] workaround sirv bug to allow routes with unicode characters
22 changes: 22 additions & 0 deletions packages/adapter-node/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
Expand All @@ -48,7 +68,9 @@ export function createServer({ render }) {
};
server.use(
compression({ threshold: 0 }),
sirv_workaround_handler,
assets_handler,
revert_sirv_workaround_handler,
prerendered_handler,
async (req, res) => {
const parsed = new URL(req.url || '', 'http://localhost');
Expand Down

0 comments on commit 511a1dc

Please sign in to comment.