-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Conversation
🦋 Changeset detectedLatest commit: 8346eb1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
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 |
globalThis.fetch = fetch; | ||
globalThis.Response = Response; | ||
globalThis.Request = Request; | ||
globalThis.Headers = Headers; | ||
Object.defineProperties(globalThis, { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Fixes #1784. Closes #1786 as its alternative.
This PR contains a breaking change to the adapter API.
Current Behavior
kit/packages/kit/src/core/build/index.js
Line 383 in 559aad5
The built
app.js
file contains a side effect of calling theinit
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
kit/packages/kit/src/core/build/index.js
Lines 288 to 292 in e2f1b3f
The
init
call is removed from the builtapp.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:kit/packages/adapter-node/src/index.js
Lines 4 to 8 in e2f1b3f
This may also be useful in the future should the
init
function need to perform other side effects.Non-breaking alternate solutions
require()
and the require shim to import the app to force it to run last:init
fromrender
the first time it's called: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.