Skip to content

Commit 15253da

Browse files
authored
feat: use web standard fetch export (#14251)
* feat: use web standard `fetch` export * fix * missing parens
1 parent 5db4cd4 commit 15253da

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

.changeset/blue-dodos-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-vercel': minor
3+
---
4+
5+
feat: use web standard `fetch` export
Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { installPolyfills } from '@sveltejs/kit/node/polyfills';
2-
import { getRequest, setResponse, createReadableStream } from '@sveltejs/kit/node';
2+
import { createReadableStream } from '@sveltejs/kit/node';
33
import { Server } from 'SERVER';
44
import { manifest } from 'MANIFEST';
55
import process from 'node:process';
@@ -15,33 +15,31 @@ await server.init({
1515

1616
const DATA_SUFFIX = '/__data.json';
1717

18-
/**
19-
* @param {import('http').IncomingMessage} req
20-
* @param {import('http').ServerResponse} res
21-
*/
22-
export default async (req, res) => {
23-
if (req.url) {
24-
const [path, search] = req.url.split('?');
25-
26-
const params = new URLSearchParams(search);
27-
let pathname = params.get('__pathname');
18+
export default {
19+
/**
20+
* @param {Request} request
21+
* @returns {Promise<Response>}
22+
*/
23+
fetch(request) {
24+
// If this is an ISR request, the requested pathname is encoded
25+
// as a search parameter, so we need to extract it
26+
const url = new URL(request.url);
27+
let pathname = url.searchParams.get('__pathname');
2828

2929
if (pathname) {
30-
params.delete('__pathname');
3130
// Optional routes' pathname replacements look like `/foo/$1/bar` which means we could end up with an url like /foo//bar
3231
pathname = pathname.replace(/\/+/g, '/');
33-
req.url = `${pathname}${path.endsWith(DATA_SUFFIX) ? DATA_SUFFIX : ''}?${params}`;
34-
}
35-
}
3632

37-
const request = await getRequest({ base: `https://${req.headers.host}`, request: req });
33+
url.pathname = pathname + (url.pathname.endsWith(DATA_SUFFIX) ? DATA_SUFFIX : '');
34+
url.searchParams.delete('__pathname');
3835

39-
void setResponse(
40-
res,
41-
await server.respond(request, {
36+
request = new Request(url, request);
37+
}
38+
39+
return server.respond(request, {
4240
getClientAddress() {
4341
return /** @type {string} */ (request.headers.get('x-forwarded-for'));
4442
}
45-
})
46-
);
43+
});
44+
}
4745
};

0 commit comments

Comments
 (0)