Skip to content

Commit

Permalink
Update adapters to use new app.render API (#3133)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Rich-Harris committed Dec 30, 2021
1 parent d17f459 commit c096899
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 24 deletions.
9 changes: 9 additions & 0 deletions .changeset/honest-beers-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@sveltejs/adapter-cloudflare': patch
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-netlify': patch
'@sveltejs/adapter-node': patch
'@sveltejs/adapter-vercel': patch
---

Update adapters to provide app.render with a url
5 changes: 5 additions & 0 deletions .changeset/rude-balloons-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Error if adapter provides wrong input to app.render
5 changes: 5 additions & 0 deletions .changeset/shy-oranges-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Replace [request|page].[origin|path|query] with url object
4 changes: 1 addition & 3 deletions packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ async function handle(event) {

// fall back to an app route
const request = event.request;
const request_url = new URL(request.url);

try {
const rendered = await app.render({
path: request_url.pathname,
query: request_url.searchParams,
url: request.url,
rawBody: await read(request),
headers: Object.fromEntries(request.headers),
method: request.method
Expand Down
3 changes: 1 addition & 2 deletions packages/adapter-cloudflare/files/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export default {
// dynamically-generated pages
try {
const rendered = await app.render({
path: url.pathname,
query: url.searchParams,
url,
rawBody: new Uint8Array(await req.arrayBuffer()),
headers: Object.fromEntries(req.headers),
method: req.method
Expand Down
7 changes: 2 additions & 5 deletions packages/adapter-netlify/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ export function init(manifest) {
const app = new App(manifest);

return async (event) => {
const { path, httpMethod, headers, rawQuery, body, isBase64Encoded } = event;

const query = new URLSearchParams(rawQuery);
const { httpMethod, headers, rawUrl, body, isBase64Encoded } = event;

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

const rendered = await app.render({
url: rawUrl,
method: httpMethod,
headers,
path,
query,
rawBody
});

Expand Down
11 changes: 1 addition & 10 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ const serve_prerendered = sirv(path.join(__dirname, '/prerendered'), {

/** @type {import('polka').Middleware} */
const ssr = async (req, res) => {
let parsed;
try {
parsed = new URL(req.url || '', 'http://localhost');
} catch (e) {
res.statusCode = 400;
return res.end('Invalid URL');
}

let body;

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

const rendered = await app.render({
url: req.url,
method: req.method,
headers: req.headers, // TODO: what about repeated headers, i.e. string[]
path: parsed.pathname,
query: parsed.searchParams,
rawBody: body
});

Expand Down
5 changes: 1 addition & 4 deletions packages/adapter-vercel/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ __fetch_polyfill();
const app = new App(manifest);

export default async (req, res) => {
const { pathname, searchParams } = new URL(req.url || '', 'http://localhost');

let body;

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

const rendered = await app.render({
url: req.url,
method: req.method,
headers: req.headers,
path: pathname,
query: searchParams,
rawBody: body
});

Expand Down
5 changes: 5 additions & 0 deletions packages/kit/src/core/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export class App {
render(request, {
prerender
} = {}) {
// TODO remove this for 1.0
if (Object.keys(request).sort().join() !== 'headers,method,rawBody,url') {
throw new Error('Adapters should call app.render({ url, method, headers, rawBody })');
}
const host = ${
config.kit.host
? s(config.kit.host)
Expand Down

0 comments on commit c096899

Please sign in to comment.