Skip to content

Commit 2f84edc

Browse files
Fix prerendered paths for adapter-node
Sharing internal utilities through `@sveltejs/kit/node` feels... odd
1 parent 502e6c2 commit 2f84edc

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

packages/adapter-node/src/handler.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import process from 'node:process';
55
import sirv from 'sirv';
66
import { fileURLToPath } from 'node:url';
77
import { parse as polka_url_parser } from '@polka/url';
8-
import { getRequest, setResponse, createReadableStream } from '@sveltejs/kit/node';
8+
import {
9+
getRequest,
10+
setResponse,
11+
createReadableStream,
12+
relative_pathname
13+
} from '@sveltejs/kit/node';
914
import { Server } from 'SERVER';
1015
import { manifest, prerendered, base } from 'MANIFEST';
1116
import { env } from 'ENV';
@@ -81,8 +86,11 @@ function serve_prerendered() {
8186
}
8287

8388
// remove or add trailing slash as appropriate
84-
let location = pathname.at(-1) === '/' ? pathname.slice(0, -1) : pathname + '/';
85-
if (prerendered.has(location)) {
89+
const inverted_trailing_slash =
90+
pathname.at(-1) === '/' ? pathname.slice(0, -1) : pathname + '/';
91+
if (prerendered.has(inverted_trailing_slash)) {
92+
// ensure preservation of (possibly invisible) path prefixes
93+
let location = relative_pathname(pathname, inverted_trailing_slash);
8694
if (query) location += search;
8795
res.writeHead(308, { location }).end();
8896
} else {

packages/kit/src/exports/node/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,5 @@ export async function setResponse(res, response) {
222222
export function createReadableStream(file) {
223223
return /** @type {ReadableStream} */ (Readable.toWeb(createReadStream(file)));
224224
}
225+
226+
export { relative_pathname } from '../../utils/url.js';

packages/kit/types/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,12 @@ declare module '@sveltejs/kit/node' {
21452145
* @since 2.4.0
21462146
*/
21472147
export function createReadableStream(file: string): ReadableStream;
2148+
/**
2149+
* Calculates the relative path between two URL pathnames.
2150+
* Note that `relative` from `node:path` works with file system paths which are subtly diffent.
2151+
* For example: it ignores trailing slashes, which are significant in URLs.
2152+
* */
2153+
export function relative_pathname(from: string, to: string): string;
21482154

21492155
export {};
21502156
}

0 commit comments

Comments
 (0)