From 0d5e85a5f5e067bdaa31496e45f16a120f88651a Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Sun, 5 Feb 2023 15:27:11 +0545 Subject: [PATCH 1/5] fix: make serve_prerendered middleware work with node http server --- packages/adapter-node/package.json | 1 + packages/adapter-node/src/handler.js | 11 ++++++++--- pnpm-lock.yaml | 6 ++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/adapter-node/package.json b/packages/adapter-node/package.json index e530fc192d28..598ff50029a1 100644 --- a/packages/adapter-node/package.json +++ b/packages/adapter-node/package.json @@ -32,6 +32,7 @@ "prepublishOnly": "pnpm build" }, "devDependencies": { + "@polka/url": "^0.5.0", "@sveltejs/kit": "workspace:^", "@types/node": "^16.18.6", "c8": "^7.12.0", diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index 650ab3ce6018..c905a5217b4c 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -3,6 +3,10 @@ import fs from 'node:fs'; import path from 'node:path'; import sirv from 'sirv'; import { fileURLToPath } from 'node:url'; + +// @ts-expect-error typedefs not available for @polka/url +import { default as polka_url_parser } from '@polka/url'; + import { getRequest, setResponse } from '@sveltejs/kit/node'; import { Server } from 'SERVER'; import { manifest, prerendered } from 'MANIFEST'; @@ -50,7 +54,8 @@ function serve_prerendered() { const handler = serve(path.join(dir, 'prerendered')); return (req, res, next) => { - let pathname = req.path; + let parsed_url = polka_url_parser(req); + let { pathname } = parsed_url; try { pathname = decodeURIComponent(pathname); @@ -65,8 +70,8 @@ function serve_prerendered() { // remove or add trailing slash as appropriate let location = pathname.at(-1) === '/' ? pathname.slice(0, -1) : pathname + '/'; if (prerendered.has(location)) { - const search = req.url.split('?')[1]; - if (search) location += `?${search}`; + const { query, search } = parsed_url; + if (query) location += search; // search is `?${query}` res.writeHead(308, { location }).end(); } else { next(); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f0be98413f85..89e106561add 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -114,6 +114,7 @@ importers: packages/adapter-node: specifiers: + '@polka/url': ^0.5.0 '@rollup/plugin-commonjs': ^24.0.0 '@rollup/plugin-json': ^6.0.0 '@rollup/plugin-node-resolve': ^15.0.1 @@ -132,6 +133,7 @@ importers: '@rollup/plugin-node-resolve': 15.0.1_rollup@3.7.0 rollup: 3.7.0 devDependencies: + '@polka/url': 0.5.0 '@sveltejs/kit': link:../kit '@types/node': 16.18.6 c8: 7.12.0 @@ -1309,6 +1311,10 @@ packages: playwright-core: 1.29.2 dev: true + /@polka/url/0.5.0: + resolution: {integrity: sha512-oZLYFEAzUKyi3SKnXvj32ZCEGH6RDnao7COuCVhDydMS9NrCSVXhM79VaKyP5+Zc33m0QXEd2DN3UkU7OsHcfw==} + dev: true + /@polka/url/1.0.0-next.21: resolution: {integrity: sha512-a5Sab1C4/icpTZVzZc5Ghpz88yQtGOyNqYXcZgOssB2uuAr+wF/MvN6bgtW32q7HHrvBki+BsZ0OuNv6EV3K9g==} From b5a258448110143e88154517beb540fa91568d33 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Sun, 5 Feb 2023 15:28:22 +0545 Subject: [PATCH 2/5] changeset --- .changeset/late-dancers-vanish.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/late-dancers-vanish.md diff --git a/.changeset/late-dancers-vanish.md b/.changeset/late-dancers-vanish.md new file mode 100644 index 000000000000..2d3e6b3951e9 --- /dev/null +++ b/.changeset/late-dancers-vanish.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-node': patch +--- + +fix: make serve_prerendered middleware work with node http server From ed9c074ef15a1c681a75effc165ef0a20207ebbb Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Sun, 5 Feb 2023 15:29:33 +0545 Subject: [PATCH 3/5] lint? --- packages/adapter-node/rollup.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/adapter-node/rollup.config.js b/packages/adapter-node/rollup.config.js index 93c9b3502f26..4c00ce5f1eb0 100644 --- a/packages/adapter-node/rollup.config.js +++ b/packages/adapter-node/rollup.config.js @@ -1,7 +1,7 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; import json from '@rollup/plugin-json'; -import { builtinModules } from 'module'; +import { builtinModules } from 'node:module'; export default [ { From 709904e1ae43d03ddad46235d2a6281a9bc2d310 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Sun, 5 Feb 2023 18:46:18 +0545 Subject: [PATCH 4/5] confusing_branches_confuse.md --- packages/adapter-node/package.json | 2 +- pnpm-lock.yaml | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/adapter-node/package.json b/packages/adapter-node/package.json index 598ff50029a1..d07c26e11639 100644 --- a/packages/adapter-node/package.json +++ b/packages/adapter-node/package.json @@ -32,7 +32,7 @@ "prepublishOnly": "pnpm build" }, "devDependencies": { - "@polka/url": "^0.5.0", + "@polka/url": "^1.0.0-next.21", "@sveltejs/kit": "workspace:^", "@types/node": "^16.18.6", "c8": "^7.12.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 89e106561add..45775bbe7bd0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -114,7 +114,7 @@ importers: packages/adapter-node: specifiers: - '@polka/url': ^0.5.0 + '@polka/url': ^1.0.0-next.21 '@rollup/plugin-commonjs': ^24.0.0 '@rollup/plugin-json': ^6.0.0 '@rollup/plugin-node-resolve': ^15.0.1 @@ -133,7 +133,7 @@ importers: '@rollup/plugin-node-resolve': 15.0.1_rollup@3.7.0 rollup: 3.7.0 devDependencies: - '@polka/url': 0.5.0 + '@polka/url': 1.0.0-next.21 '@sveltejs/kit': link:../kit '@types/node': 16.18.6 c8: 7.12.0 @@ -1311,10 +1311,6 @@ packages: playwright-core: 1.29.2 dev: true - /@polka/url/0.5.0: - resolution: {integrity: sha512-oZLYFEAzUKyi3SKnXvj32ZCEGH6RDnao7COuCVhDydMS9NrCSVXhM79VaKyP5+Zc33m0QXEd2DN3UkU7OsHcfw==} - dev: true - /@polka/url/1.0.0-next.21: resolution: {integrity: sha512-a5Sab1C4/icpTZVzZc5Ghpz88yQtGOyNqYXcZgOssB2uuAr+wF/MvN6bgtW32q7HHrvBki+BsZ0OuNv6EV3K9g==} From 87c34d838623447c93b860cc68c95df0eec6bb30 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Sun, 5 Feb 2023 18:49:56 +0545 Subject: [PATCH 5/5] simplify --- packages/adapter-node/src/handler.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index c905a5217b4c..331b6cccaff2 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -3,10 +3,7 @@ import fs from 'node:fs'; import path from 'node:path'; import sirv from 'sirv'; import { fileURLToPath } from 'node:url'; - -// @ts-expect-error typedefs not available for @polka/url -import { default as polka_url_parser } from '@polka/url'; - +import { parse as polka_url_parser } from '@polka/url'; import { getRequest, setResponse } from '@sveltejs/kit/node'; import { Server } from 'SERVER'; import { manifest, prerendered } from 'MANIFEST'; @@ -54,8 +51,7 @@ function serve_prerendered() { const handler = serve(path.join(dir, 'prerendered')); return (req, res, next) => { - let parsed_url = polka_url_parser(req); - let { pathname } = parsed_url; + let { pathname, search, query } = polka_url_parser(req); try { pathname = decodeURIComponent(pathname); @@ -70,8 +66,7 @@ function serve_prerendered() { // remove or add trailing slash as appropriate let location = pathname.at(-1) === '/' ? pathname.slice(0, -1) : pathname + '/'; if (prerendered.has(location)) { - const { query, search } = parsed_url; - if (query) location += search; // search is `?${query}` + if (query) location += search; res.writeHead(308, { location }).end(); } else { next();