From 7b1d40ddb2ff1b8c9abac29a0261483da3fd24de Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Fri, 13 Aug 2021 11:03:57 -0700 Subject: [PATCH 1/4] [fix] workaround sirv bug --- .changeset/tame-lions-jump.md | 5 +++++ packages/adapter-node/src/server.js | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 .changeset/tame-lions-jump.md diff --git a/.changeset/tame-lions-jump.md b/.changeset/tame-lions-jump.md new file mode 100644 index 000000000000..94c611bdb81b --- /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 0e538042935c..4ad7284aef77 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'); From 8450681e25ffa36dc5fb07abdf37e341454b17ec Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Fri, 13 Aug 2021 15:14:32 -0700 Subject: [PATCH 2/4] upgrade polka --- packages/adapter-node/package.json | 2 +- packages/adapter-node/src/server.js | 11 +---------- pnpm-lock.yaml | 8 ++++---- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/packages/adapter-node/package.json b/packages/adapter-node/package.json index ffe1617692c9..c360eb88f732 100644 --- a/packages/adapter-node/package.json +++ b/packages/adapter-node/package.json @@ -31,7 +31,7 @@ "c8": "^7.7.2", "compression": "^1.7.4", "node-fetch": "^3.0.0-beta.9", - "polka": "^1.0.0-next.15", + "polka": "^1.0.0-next.16", "rollup": "^2.55.0", "sirv": "^1.0.12", "uvu": "^0.5.1" diff --git a/packages/adapter-node/src/server.js b/packages/adapter-node/src/server.js index 4ad7284aef77..25090b4b3f61 100644 --- a/packages/adapter-node/src/server.js +++ b/packages/adapter-node/src/server.js @@ -57,16 +57,7 @@ export function createServer({ render }) { } : 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 - server.parse = (req) => { - return parse(req, false); - }; - server.use( + const server = polka().use( compression({ threshold: 0 }), sirv_workaround_handler, assets_handler, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 81ac37d79b99..e0f039be2806 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -95,7 +95,7 @@ importers: compression: ^1.7.4 esbuild: ^0.12.5 node-fetch: ^3.0.0-beta.9 - polka: ^1.0.0-next.15 + polka: ^1.0.0-next.16 rollup: ^2.55.0 sirv: ^1.0.12 tiny-glob: ^0.2.9 @@ -110,7 +110,7 @@ importers: c8: 7.7.2 compression: 1.7.4 node-fetch: 3.0.0-beta.9 - polka: 1.0.0-next.15 + polka: 1.0.0-next.16 rollup: 2.55.0 sirv: 1.0.12 uvu: 0.5.1 @@ -3015,8 +3015,8 @@ packages: engines: {node: '>=10.13.0'} dev: true - /polka/1.0.0-next.15: - resolution: {integrity: sha512-zBCZO40+USkSj0GDHMqufthqk4TIRc9xVGd50LbMvYNEwGHK8dZczLBQtw9pPHBM+i/Xg7ed7+c+r6J68XkWLg==} + /polka/1.0.0-next.16: + resolution: {integrity: sha512-oTipKVQSdyugiPpU13pliPkjrycwfiiUf3y9qKCZ2oQlxb1GEkJ3lGqSDFNGF8B2x58bPrW1Lef7IYwE9iu4IQ==} engines: {node: '>=8'} dependencies: '@polka/url': 1.0.0-next.15 From 6109a451b900d6c6378c92fabc2ce64393859e4f Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Fri, 13 Aug 2021 15:51:06 -0700 Subject: [PATCH 3/4] upgrade sirv --- .changeset/tame-lions-jump.md | 2 +- packages/adapter-node/package.json | 2 +- packages/adapter-node/src/server.js | 20 -------------------- pnpm-lock.yaml | 13 +++++++++++-- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/.changeset/tame-lions-jump.md b/.changeset/tame-lions-jump.md index 94c611bdb81b..84ddf47767ec 100644 --- a/.changeset/tame-lions-jump.md +++ b/.changeset/tame-lions-jump.md @@ -2,4 +2,4 @@ '@sveltejs/adapter-node': patch --- -[fix] workaround sirv bug to allow routes with unicode characters +[fix] upgrade polka and sirv. fixes handling of URLs with unicode characters diff --git a/packages/adapter-node/package.json b/packages/adapter-node/package.json index c360eb88f732..3296cd47b7e5 100644 --- a/packages/adapter-node/package.json +++ b/packages/adapter-node/package.json @@ -33,7 +33,7 @@ "node-fetch": "^3.0.0-beta.9", "polka": "^1.0.0-next.16", "rollup": "^2.55.0", - "sirv": "^1.0.12", + "sirv": "^1.0.13", "uvu": "^0.5.1" } } diff --git a/packages/adapter-node/src/server.js b/packages/adapter-node/src/server.js index 25090b4b3f61..3055d70425e4 100644 --- a/packages/adapter-node/src/server.js +++ b/packages/adapter-node/src/server.js @@ -26,17 +26,6 @@ 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) => { @@ -50,19 +39,10 @@ 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().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'); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e0f039be2806..7ed61e51c378 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -97,7 +97,7 @@ importers: node-fetch: ^3.0.0-beta.9 polka: ^1.0.0-next.16 rollup: ^2.55.0 - sirv: ^1.0.12 + sirv: ^1.0.13 tiny-glob: ^0.2.9 uvu: ^0.5.1 dependencies: @@ -112,7 +112,7 @@ importers: node-fetch: 3.0.0-beta.9 polka: 1.0.0-next.16 rollup: 2.55.0 - sirv: 1.0.12 + sirv: 1.0.13 uvu: 0.5.1 packages/adapter-static: @@ -3356,6 +3356,15 @@ packages: totalist: 1.1.0 dev: true + /sirv/1.0.13: + resolution: {integrity: sha512-J6AeSdfF0+QBqkuLb8IKv93bOn57U/YjVqlFLQITwgHZF27Asgbuu8Vyx4RM3TPUnTojCLh8C2krFe6xTfxRjA==} + engines: {node: '>= 10'} + dependencies: + '@polka/url': 1.0.0-next.15 + mime: 2.5.2 + totalist: 1.1.0 + dev: true + /sisteransi/1.0.5: resolution: {integrity: sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==} dev: false From 0b07795ed62eb7a1d99e171b543f2bf3c3b9a293 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Fri, 13 Aug 2021 15:53:28 -0700 Subject: [PATCH 4/4] remove unused import --- packages/adapter-node/package.json | 1 - packages/adapter-node/src/server.js | 1 - pnpm-lock.yaml | 2 -- 3 files changed, 4 deletions(-) diff --git a/packages/adapter-node/package.json b/packages/adapter-node/package.json index 3296cd47b7e5..88c9ea06306e 100644 --- a/packages/adapter-node/package.json +++ b/packages/adapter-node/package.json @@ -25,7 +25,6 @@ "tiny-glob": "^0.2.9" }, "devDependencies": { - "@polka/url": "^1.0.0-next.15", "@rollup/plugin-json": "^4.1.0", "@sveltejs/kit": "workspace:*", "c8": "^7.7.2", diff --git a/packages/adapter-node/src/server.js b/packages/adapter-node/src/server.js index 3055d70425e4..4dc6c69aa209 100644 --- a/packages/adapter-node/src/server.js +++ b/packages/adapter-node/src/server.js @@ -3,7 +3,6 @@ import compression from 'compression'; import fs from 'fs'; import { dirname, join } from 'path'; import polka from 'polka'; -import { parse } from '@polka/url'; import sirv from 'sirv'; import { fileURLToPath } from 'url'; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7ed61e51c378..e0daea9adf4a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -88,7 +88,6 @@ importers: packages/adapter-node: specifiers: - '@polka/url': ^1.0.0-next.15 '@rollup/plugin-json': ^4.1.0 '@sveltejs/kit': workspace:* c8: ^7.7.2 @@ -104,7 +103,6 @@ importers: esbuild: 0.12.5 tiny-glob: 0.2.9 devDependencies: - '@polka/url': 1.0.0-next.15 '@rollup/plugin-json': 4.1.0_rollup@2.55.0 '@sveltejs/kit': link:../kit c8: 7.7.2