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

Conversation

GrygrFlzr
Copy link
Member

Fixes #1784. Closes #1786 as its alternative.

This PR contains a breaking change to the adapter API.

Current Behavior

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

The built app.js file contains a side effect of calling the init function, which ends up running before polyfills like @sveltejs/kit/install-fetch. Although ESM (supposedly) guarantees a top-down order for import side effects, this is currently not necessarily true with esbuild nor rollup (see evanw/esbuild#399 (comment)).

Proposed Behavior

const default_settings = { paths: ${s(config.kit.paths)} };
// allow paths to be overridden in svelte-kit preview
// and in prerendering
export function init(settings = default_settings) {

The init call is removed from the built app.js, and instead it becomes the responsibility of the adapters to call it at least once. This lets it control when the initialization happens, allowing us to guarantee that it happens after any polyfills:

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, () => {

This may also be useful in the future should the init function need to perform other side effects.

Non-breaking alternate solutions

  1. Use require() and the require shim to import the app to force it to run last:
    const { render } = require('../output/server/app.js');
    This is not a great long-term solution considering we're forging ahead with ESM anyway.
  2. Call init from render the first time it's called:
    let initialized = false;
    export function render(/* ... */) {
      if (!initialized) {
        initialized = true;
        init(/* ... */);
      }
      // ...
    }
    In environments such as adapter-node it won't be evaluated until the server handles the quest, which may delay the initial response. May also affect serverless environments where lambdas may live longer than a single request.

@changeset-bot
Copy link

changeset-bot bot commented Jul 2, 2021

🦋 Changeset detected

Latest commit: 8346eb1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@sveltejs/adapter-cloudflare-workers Patch
@sveltejs/adapter-netlify Patch
@sveltejs/adapter-node Patch
@sveltejs/adapter-vercel Patch
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines -4 to +3
globalThis.fetch = fetch;
globalThis.Response = Response;
globalThis.Request = Request;
globalThis.Headers = Headers;
Object.defineProperties(globalThis, {
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.

@@ -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.

@GrygrFlzr GrygrFlzr added the adapters - general Support for functionality general to all adapters label Jul 2, 2021
@benmccann benmccann added the bug Something isn't working label Jul 2, 2021
Copy link
Member

@benmccann benmccann left a comment

Choose a reason for hiding this comment

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

Thank you!

packages/adapter-netlify/files/entry.js Show resolved Hide resolved
@Rich-Harris Rich-Harris merged commit 9f0c54a into master Jul 2, 2021
@Rich-Harris Rich-Harris deleted the externalize-side-effects branch July 2, 2021 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters - general Support for functionality general to all adapters bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ReferenceError: fetch is not defined
3 participants