remove site requirement from netlify adapter (#3041)
* remove site requirement from netlify adapter * update readme
This commit is contained in:
parent
d63dcd505a
commit
fbc32cf655
3 changed files with 18 additions and 26 deletions
|
@ -10,10 +10,6 @@ import netlify from '@astrojs/netlify/functions';
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
adapter: netlify(),
|
adapter: netlify(),
|
||||||
// Where your Netlify app will be deployed.
|
|
||||||
// Feel free to use a local URL (i.e. http://localhost:8080)
|
|
||||||
// to test local builds via the netlify CLI
|
|
||||||
site: 'https://my-production-url.netlify.app',
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import type { AstroAdapter, AstroIntegration, AstroConfig } from 'astro';
|
import type { AstroAdapter, AstroIntegration, AstroConfig } from 'astro';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
|
||||||
export function getAdapter(site: string | undefined): AstroAdapter {
|
export function getAdapter(): AstroAdapter {
|
||||||
return {
|
return {
|
||||||
name: '@astrojs/netlify',
|
name: '@astrojs/netlify',
|
||||||
serverEntrypoint: '@astrojs/netlify/netlify-functions.js',
|
serverEntrypoint: '@astrojs/netlify/netlify-functions.js',
|
||||||
exports: ['handler'],
|
exports: ['handler'],
|
||||||
args: { site },
|
args: { },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,15 +28,7 @@ function netlifyFunctions({ dist }: NetlifyFunctionsOptions = {}): AstroIntegrat
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'astro:config:done': ({ config, setAdapter }) => {
|
'astro:config:done': ({ config, setAdapter }) => {
|
||||||
let site = null;
|
setAdapter(getAdapter());
|
||||||
try {
|
|
||||||
site = new URL(config.base, config.site);
|
|
||||||
} catch {
|
|
||||||
throw new Error(
|
|
||||||
'The Netlify adapter requires a deployment URL. Ensure a "site" is specified in your astro.config. If you provided a "base" in your astro.config, ensure it is a valid path.'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
setAdapter(getAdapter(site.toString()));
|
|
||||||
_config = config;
|
_config = config;
|
||||||
},
|
},
|
||||||
'astro:build:start': async ({ buildConfig }) => {
|
'astro:build:start': async ({ buildConfig }) => {
|
||||||
|
|
|
@ -7,20 +7,24 @@ polyfill(globalThis, {
|
||||||
exclude: 'window document',
|
exclude: 'window document',
|
||||||
});
|
});
|
||||||
|
|
||||||
interface Args {
|
interface Args {}
|
||||||
site?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const createExports = (manifest: SSRManifest, args: Args) => {
|
export const createExports = (manifest: SSRManifest, args: Args) => {
|
||||||
const app = new App(manifest);
|
const app = new App(manifest);
|
||||||
const site = new URL(args.site ?? `https://netlify.com`);
|
|
||||||
|
|
||||||
const handler: Handler = async (event) => {
|
const handler: Handler = async (event) => {
|
||||||
const headers = new Headers(event.headers as any);
|
const { httpMethod, headers, rawUrl, body: requestBody, isBase64Encoded } = event;
|
||||||
const request = new Request(new URL(event.path, site).toString(), {
|
const init: RequestInit = {
|
||||||
method: event.httpMethod,
|
method: httpMethod,
|
||||||
headers,
|
headers: new Headers(headers as any),
|
||||||
});
|
};
|
||||||
|
// Attach the event body the the request, with proper encoding.
|
||||||
|
if (httpMethod !== 'GET' && httpMethod !== 'HEAD') {
|
||||||
|
const encoding = isBase64Encoded ? 'base64' : 'utf-8';
|
||||||
|
init.body =
|
||||||
|
typeof requestBody === 'string' ? Buffer.from(requestBody, encoding) : requestBody;
|
||||||
|
}
|
||||||
|
const request = new Request(rawUrl, init);
|
||||||
|
|
||||||
if (!app.match(request)) {
|
if (!app.match(request)) {
|
||||||
return {
|
return {
|
||||||
|
@ -30,12 +34,12 @@ export const createExports = (manifest: SSRManifest, args: Args) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await app.render(request);
|
const response = await app.render(request);
|
||||||
const body = await response.text();
|
const responseBody = await response.text();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
headers: Object.fromEntries(response.headers.entries()),
|
headers: Object.fromEntries(response.headers.entries()),
|
||||||
body,
|
body: responseBody,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue