Skip to content

Commit

Permalink
fix: serve_prerendered breaks on node http server (#8890)
Browse files Browse the repository at this point in the history
* fix: make serve_prerendered middleware work with node http server

* changeset

* lint?

* confusing_branches_confuse.md

* simplify
  • Loading branch information
gtm-nayan authored Feb 6, 2023
1 parent d1f7655 commit 66ae0a2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
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",
"@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.

0 comments on commit 66ae0a2

Please sign in to comment.