Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Externalize app initialization to adapters #1804

Merged
merged 4 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/polite-ducks-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-netlify': patch
'@sveltejs/adapter-node': patch
'@sveltejs/adapter-vercel': patch
'@sveltejs/kit': patch
---

Externalize app initialization to adapters
2 changes: 2 additions & 0 deletions packages/adapter-begin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import url from 'url';
import app from '@architect/shared/app.js'; // eslint-disable-line import/no-unresolved

// TODO: run init() on the app before handling routes

async function handler(event) {
const { host, rawPath: path, httpMethod, rawQueryString, headers, body } = event;

Expand Down
4 changes: 3 additions & 1 deletion packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// TODO hardcoding the relative location makes this brittle
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler'; // eslint-disable-line import/no-unresolved

init();

addEventListener('fetch', (event) => {
event.respondWith(handle(event));
});
Expand Down
4 changes: 3 additions & 1 deletion packages/adapter-netlify/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved

// TODO hardcoding the relative location makes this brittle
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved

init();
GrygrFlzr marked this conversation as resolved.
Show resolved Hide resolved

export async function handler(event) {
const { path, httpMethod, headers, rawQuery, body, isBase64Encoded } = event;
Expand Down
4 changes: 3 additions & 1 deletion packages/adapter-node/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import './require_shim';
import { createServer } from './server';
// TODO hardcoding the relative location makes this brittle
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { host, port } from './env.js'; // eslint-disable-line import/no-unresolved

init();

const instance = createServer({ render }).listen(port, host, () => {
console.log(`Listening on port ${port}`);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/src/require_shim.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { createRequire } from 'module';
global.require = createRequire(import.meta.url);
globalThis.require = createRequire(import.meta.url);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency with the rest of the codebase.

4 changes: 3 additions & 1 deletion packages/adapter-vercel/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { getRawBody } from '@sveltejs/kit/node'; // eslint-disable-line import/n
import '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved

// TODO hardcoding the relative location makes this brittle
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved

init();

export default async (req, res) => {
const { pathname, searchParams } = new URL(req.url || '', 'http://localhost');
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,11 @@ async function build_server(

let options = null;

const default_settings = { paths: ${s(config.kit.paths)} };

// allow paths to be overridden in svelte-kit preview
// and in prerendering
export function init(settings) {
export function init(settings = default_settings) {
set_paths(settings.paths);
set_prerendering(settings.prerendering || false);

Expand Down Expand Up @@ -380,8 +382,6 @@ async function build_server(
};
}

init({ paths: ${s(config.kit.paths)} });

export function render(request, {
prerender
} = {}) {
Expand Down
23 changes: 18 additions & 5 deletions packages/kit/src/install-fetch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
// @ts-nocheck
import fetch, { Response, Request, Headers } from 'node-fetch';

globalThis.fetch = fetch;
globalThis.Response = Response;
globalThis.Request = Request;
globalThis.Headers = Headers;
Object.defineProperties(globalThis, {
Comment on lines -4 to +3
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defineProperties seems like a more robust solution, and also avoids having to slap ts-nocheck directives.

fetch: {
enumerable: true,
value: fetch
},
Response: {
enumerable: true,
value: Response
},
Request: {
enumerable: true,
value: Request
},
Headers: {
enumerable: true,
value: Headers
}
});