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 polyfills not being available to imports on Netlify #6117

Merged
merged 10 commits into from
Feb 6, 2023
5 changes: 5 additions & 0 deletions .changeset/gold-socks-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/webapi': patch
---

Add `polyfill-ssr.js` file to apply polyfills needed for certain SSR contexts as a side effect
5 changes: 5 additions & 0 deletions .changeset/silent-dragons-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/netlify': patch
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
---

Fix polyfills not being available in certain cases
2 changes: 2 additions & 0 deletions packages/integrations/netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"./netlify-functions.js": "./dist/netlify-functions.js",
"./edge-functions": "./dist/integration-edge-functions.js",
"./netlify-edge-functions.js": "./dist/netlify-edge-functions.js",
"./polyfill.js": "./dist/polyfill.js",
"./polyfill": "./dist/polyfill.js",
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
"./package.json": "./package.json"
},
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions packages/integrations/netlify/src/integration-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ function netlifyFunctions({
client: outDir,
server: new URL('./.netlify/functions-internal/', config.root),
},
vite: {
ssr: {
external: ['@astrojs/webapi/polyfill-ssr.js'],
},
},
});
},
'astro:config:done': ({ config, setAdapter }) => {
Expand Down
7 changes: 2 additions & 5 deletions packages/integrations/netlify/src/netlify-functions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { polyfill } from '@astrojs/webapi';
import '@astrojs/webapi/polyfill-ssr.js';
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved

import { builder, Handler } from '@netlify/functions';
import { SSRManifest } from 'astro';
import { App } from 'astro/app';

polyfill(globalThis, {
exclude: 'window document',
});

export interface Args {
builders?: boolean;
binaryMediaTypes?: string[];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

<div>{typeof fetch}</div>
28 changes: 28 additions & 0 deletions packages/integrations/netlify/test/functions/polyfills.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect } from 'chai';
import netlifyAdapter from '../../dist/index.js';
import { loadFixture, testIntegration } from './test-utils.js';

describe('Support polyfills', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/polyfills/', import.meta.url).toString(),
output: 'server',
adapter: netlifyAdapter({
dist: new URL('./fixtures/polyfills/dist/', import.meta.url),
}),
site: `http://example.com`,
integrations: [testIntegration()],
});
await fixture.build();
});

it('starts with the polyfill import', async () => {
const entry = await fixture.readFile('../.netlify/functions-internal/entry.mjs');
expect(entry).to.satisfy((content) =>
content.startsWith("import '@astrojs/webapi/polyfill-ssr.js';")
);
});
});
6 changes: 5 additions & 1 deletion packages/webapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
},
"./apply": {
"import": "./apply.js"
},
"./polyfill-ssr.js": {
"import": "./polyfill-ssr.js"
}
},
"main": "mod.js",
Expand All @@ -18,7 +21,8 @@
"apply.js",
"mod.d.ts",
"mod.js",
"mod.js.map"
"mod.js.map",
"polyfill-ssr.js"
],
"keywords": [
"astro",
Expand Down
7 changes: 7 additions & 0 deletions packages/webapi/polyfill-ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { polyfill } from './mod.js'

export * from './mod.js'

polyfill(globalThis, {
exclude: 'window document',
})