Skip to content

Commit c096899

Browse files
authored
Update adapters to use new app.render API (#3133)
* replace request.origin/path/query with request.url * fix some stuff * typo * lint * fix test * fix xss vuln * fix test * gah * fixes * simplify * use pathname+search as key * all tests passing * lint * tidy up * update types in docs * update docs * more docs * more docs * more docs * i would be lost without conduitry * update template app * throw useful error when trying to access path/query/origin/page * $params -> $route.params, only use getters in dev, remove the word "deprecate" * update docs * finish updating load input section * update * make this section less verbose * move url into $route * update some docs * rename route store back to page * throw errors on accessing $page.path etc * fix types * fix some docs * fix migrating docs * decode path, strip prefix * tidy up * remove comment * lint * update adapters * changeset * error if adapter provides wrong input * changeset * Update documentation/docs/05-modules.md * Update documentation/migrating/04-pages.md * remove unused code
1 parent d17f459 commit c096899

File tree

9 files changed

+30
-24
lines changed

9 files changed

+30
-24
lines changed

.changeset/honest-beers-sing.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': patch
3+
'@sveltejs/adapter-cloudflare-workers': patch
4+
'@sveltejs/adapter-netlify': patch
5+
'@sveltejs/adapter-node': patch
6+
'@sveltejs/adapter-vercel': patch
7+
---
8+
9+
Update adapters to provide app.render with a url

.changeset/rude-balloons-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Error if adapter provides wrong input to app.render

.changeset/shy-oranges-sin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Replace [request|page].[origin|path|query] with url object

packages/adapter-cloudflare-workers/files/entry.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ async function handle(event) {
2626

2727
// fall back to an app route
2828
const request = event.request;
29-
const request_url = new URL(request.url);
3029

3130
try {
3231
const rendered = await app.render({
33-
path: request_url.pathname,
34-
query: request_url.searchParams,
32+
url: request.url,
3533
rawBody: await read(request),
3634
headers: Object.fromEntries(request.headers),
3735
method: request.method

packages/adapter-cloudflare/files/worker.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ export default {
3333
// dynamically-generated pages
3434
try {
3535
const rendered = await app.render({
36-
path: url.pathname,
37-
query: url.searchParams,
36+
url,
3837
rawBody: new Uint8Array(await req.arrayBuffer()),
3938
headers: Object.fromEntries(req.headers),
4039
method: req.method

packages/adapter-netlify/src/handler.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@ export function init(manifest) {
77
const app = new App(manifest);
88

99
return async (event) => {
10-
const { path, httpMethod, headers, rawQuery, body, isBase64Encoded } = event;
11-
12-
const query = new URLSearchParams(rawQuery);
10+
const { httpMethod, headers, rawUrl, body, isBase64Encoded } = event;
1311

1412
const encoding = isBase64Encoded ? 'base64' : headers['content-encoding'] || 'utf-8';
1513
const rawBody = typeof body === 'string' ? Buffer.from(body, encoding) : body;
1614

1715
const rendered = await app.render({
16+
url: rawUrl,
1817
method: httpMethod,
1918
headers,
20-
path,
21-
query,
2219
rawBody
2320
});
2421

packages/adapter-node/src/handler.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ const serve_prerendered = sirv(path.join(__dirname, '/prerendered'), {
3939

4040
/** @type {import('polka').Middleware} */
4141
const ssr = async (req, res) => {
42-
let parsed;
43-
try {
44-
parsed = new URL(req.url || '', 'http://localhost');
45-
} catch (e) {
46-
res.statusCode = 400;
47-
return res.end('Invalid URL');
48-
}
49-
5042
let body;
5143

5244
try {
@@ -57,10 +49,9 @@ const ssr = async (req, res) => {
5749
}
5850

5951
const rendered = await app.render({
52+
url: req.url,
6053
method: req.method,
6154
headers: req.headers, // TODO: what about repeated headers, i.e. string[]
62-
path: parsed.pathname,
63-
query: parsed.searchParams,
6455
rawBody: body
6556
});
6657

packages/adapter-vercel/files/entry.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ __fetch_polyfill();
88
const app = new App(manifest);
99

1010
export default async (req, res) => {
11-
const { pathname, searchParams } = new URL(req.url || '', 'http://localhost');
12-
1311
let body;
1412

1513
try {
@@ -20,10 +18,9 @@ export default async (req, res) => {
2018
}
2119

2220
const rendered = await app.render({
21+
url: req.url,
2322
method: req.method,
2423
headers: req.headers,
25-
path: pathname,
26-
query: searchParams,
2724
rawBody: body
2825
});
2926

packages/kit/src/core/build/build_server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ export class App {
8888
render(request, {
8989
prerender
9090
} = {}) {
91+
// TODO remove this for 1.0
92+
if (Object.keys(request).sort().join() !== 'headers,method,rawBody,url') {
93+
throw new Error('Adapters should call app.render({ url, method, headers, rawBody })');
94+
}
95+
9196
const host = ${
9297
config.kit.host
9398
? s(config.kit.host)

0 commit comments

Comments
 (0)