Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serve_prerendered breaks on node http server #8890

Merged
merged 5 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/late-dancers-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

fix: make serve_prerendered middleware work with node http server
1 change: 1 addition & 0 deletions packages/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"prepublishOnly": "pnpm build"
},
"devDependencies": {
"@polka/url": "^1.0.0-next.21",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need to be a regular dependency if we're using it in the code the user gets?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because it gets bundled.

"@sveltejs/kit": "workspace:^",
"@types/node": "^16.18.6",
"c8": "^7.12.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/rollup.config.js
Original file line number Diff line number Diff line change
@@ -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 [
{
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'node:fs';
import path from 'node:path';
import sirv from 'sirv';
import { fileURLToPath } from 'node: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';
Expand Down Expand Up @@ -50,7 +51,7 @@ function serve_prerendered() {
const handler = serve(path.join(dir, 'prerendered'));

return (req, res, next) => {
let pathname = req.path;
let { pathname, search, query } = polka_url_parser(req);

try {
pathname = decodeURIComponent(pathname);
Expand All @@ -65,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 search = req.url.split('?')[1];
if (search) location += `?${search}`;
if (query) location += search;
res.writeHead(308, { location }).end();
} else {
next();
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.