Skip to content

Commit

Permalink
Apply polyfills right away (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Aug 15, 2024
1 parent cd4c084 commit 2248bc7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/thirty-panthers-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astrojs/netlify': patch
---

Apply polyfills immediately on function execution

This moves up when the polyfills are applied so that they are present before Astro runs, preventing a race condition that can cause `crypto` to not be defined early enough in Node 18.
4 changes: 2 additions & 2 deletions packages/netlify/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randomUUID } from 'node:crypto';
import { appendFile, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { appendFile, mkdir, readFile, writeFile } from 'node:fs/promises';
import type { IncomingMessage } from 'node:http';
import { fileURLToPath } from 'node:url';
import { emptyDir } from '@astrojs/internal-helpers/fs';
Expand Down Expand Up @@ -495,7 +495,7 @@ export default function netlifyIntegration(

// local dev
'astro:server:setup': async ({ server }) => {
server.middlewares.use((req, res, next) => {
server.middlewares.use((req, _res, next) => {
const locals = Symbol.for('astro.locals');
Reflect.set(req, locals, {
...Reflect.get(req, locals),
Expand Down
4 changes: 2 additions & 2 deletions packages/netlify/src/ssr-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
import { applyPolyfills } from 'astro/app/node';

applyPolyfills();

// Won't throw if the virtual module is not available because it's not supported in
// the users's astro version or if astro:env is not enabled in the project
await import('astro/env/setup')
.then((mod) => mod.setGetEnv((key) => process.env[key]))
.catch(() => {});

applyPolyfills();

export interface Args {
middlewareSecret: string;
}
Expand Down

0 comments on commit 2248bc7

Please sign in to comment.