1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
|
import { randomUUID } from 'node:crypto';
import { appendFile, mkdir, readFile, writeFile } from 'node:fs/promises';
import type { IncomingMessage } from 'node:http';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { emptyDir } from '@astrojs/internal-helpers/fs';
import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
import type { Context } from '@netlify/functions';
import type {
AstroConfig,
AstroIntegration,
AstroIntegrationLogger,
HookParameters,
IntegrationResolvedRoute,
} from 'astro';
import { build } from 'esbuild';
import glob from 'fast-glob';
import { copyDependenciesToFunction } from './lib/nft.js';
import type { Args } from './ssr-function.js';
const { version: packageVersion } = JSON.parse(
await readFile(new URL('../package.json', import.meta.url), 'utf8'),
);
export interface NetlifyLocals {
netlify: {
context: Context;
};
}
type RemotePattern = AstroConfig['image']['remotePatterns'][number];
/**
* Convert a remote pattern object to a regex string
*/
export function remotePatternToRegex(
pattern: RemotePattern,
logger: AstroIntegrationLogger,
): string | undefined {
let { protocol, hostname, port, pathname } = pattern;
let regexStr = '';
if (protocol) {
regexStr += `${protocol}://`;
} else {
// Default to matching any protocol
regexStr += '[a-z]+://';
}
if (hostname) {
if (hostname.startsWith('**.')) {
// match any number of subdomains
regexStr += '([a-z0-9-]+\\.)*';
hostname = hostname.substring(3);
} else if (hostname.startsWith('*.')) {
// match one subdomain
regexStr += '([a-z0-9-]+\\.)?';
hostname = hostname.substring(2); // Remove '*.' from the beginning
}
// Escape dots in the hostname
regexStr += hostname.replace(/\./g, '\\.');
} else {
regexStr += '[a-z0-9.-]+';
}
if (port) {
regexStr += `:${port}`;
} else {
// Default to matching any port
regexStr += '(:[0-9]+)?';
}
if (pathname) {
if (pathname.endsWith('/**')) {
// Match any path.
regexStr += `(\\${pathname.replace('/**', '')}.*)`;
}
if (pathname.endsWith('/*')) {
// Match one level of path
regexStr += `(\\${pathname.replace('/*', '')}\/[^/?#]+)\/?`;
} else {
// Exact match
regexStr += `(\\${pathname})`;
}
} else {
// Default to matching any path
regexStr += '(\\/[^?#]*)?';
}
if (!regexStr.endsWith('.*)')) {
// Match query, but only if it's not already matched by the pathname
regexStr += '([?][^#]*)?';
}
try {
new RegExp(regexStr);
} catch {
logger.warn(
`Could not generate a valid regex from the remotePattern "${JSON.stringify(
pattern,
)}". Please check the syntax.`,
);
return undefined;
}
return regexStr;
}
async function writeNetlifyFrameworkConfig(config: AstroConfig, logger: AstroIntegrationLogger) {
const remoteImages: Array<string> = [];
// Domains get a simple regex match
remoteImages.push(
...config.image.domains.map((domain) => `https?:\/\/${domain.replaceAll('.', '\\.')}\/.*`),
);
// Remote patterns need to be converted to regexes
remoteImages.push(
...config.image.remotePatterns
.map((pattern) => remotePatternToRegex(pattern, logger))
.filter(Boolean as unknown as (pattern?: string) => pattern is string),
);
const headers = config.build.assetsPrefix
? undefined
: [
{
for: `${config.base}${config.base.endsWith('/') ? '' : '/'}${config.build.assets}/*`,
values: {
'Cache-Control': 'public, max-age=31536000, immutable',
},
},
];
// See https://docs.netlify.com/image-cdn/create-integration/
const deployConfigDir = new URL('.netlify/v1/', config.root);
await mkdir(deployConfigDir, { recursive: true });
await writeFile(
new URL('./config.json', deployConfigDir),
JSON.stringify({
images: { remote_images: remoteImages },
headers,
}),
);
}
export interface NetlifyIntegrationConfig {
/**
* Force files to be bundled with your SSR function.
* This is useful for including any type of file that is not directly detected by the bundler,
* like configuration files or assets that are dynamically imported at runtime.
*
* Note: File paths are resolved relative to your project's `root`. Absolute paths may not work as expected.
*
* @example
* ```js
* includeFiles: ['./src/data/*.json', './src/locales/*.yml', './src/config/*.yaml']
* ```
*/
includeFiles?: string[];
/**
* Exclude files from the bundling process.
* This is useful for excluding any type of file that is not intended to be bundled with your SSR function,
* such as large assets, temporary files, or sensitive local configuration files.
*
* @example
* ```js
* excludeFiles: ['./src/secret/*.json', './src/temp/*.txt']
* ```
*/
excludeFiles?: string[];
/**
* If enabled, On-Demand-Rendered pages are cached for up to a year.
* This is useful for pages that are not updated often, like a blog post,
* but that you have too many of to pre-render at build time.
*
* You can override this behavior on a per-page basis
* by setting the `Cache-Control`, `CDN-Cache-Control` or `Netlify-CDN-Cache-Control` header
* from within the Page:
*
* ```astro
* // src/pages/cached-clock.astro
* Astro.response.headers.set('CDN-Cache-Control', "public, max-age=45, must-revalidate");
* ---
* <p>{Date.now()}</p>
* ```
*/
cacheOnDemandPages?: boolean;
/**
* If disabled, Middleware is applied to prerendered pages at build-time, and to on-demand-rendered pages at runtime.
* Only disable when your Middleware does not need to run on prerendered pages.
* If you use Middleware to implement authentication, redirects or similar things, you should should likely enabled it.
*
* If enabled, Astro Middleware is deployed as an Edge Function and applies to all routes.
* Caveat: Locals set in Middleware are not applied to prerendered pages, because they've been rendered at build-time and are served from the CDN.
*
* @default {false}
*/
edgeMiddleware?: boolean;
/**
* If enabled, Netlify Image CDN is used for image optimization.
* This transforms images on-the-fly without impacting build times.
*
* If disabled, Astro's built-in image optimization is run at build-time instead.
*
* @default {true}
*/
imageCDN?: boolean;
}
export default function netlifyIntegration(
integrationConfig?: NetlifyIntegrationConfig,
): AstroIntegration {
const isRunningInNetlify = Boolean(
process.env.NETLIFY || process.env.NETLIFY_LOCAL || process.env.NETLIFY_DEV,
);
let _config: AstroConfig;
let outDir: URL;
let rootDir: URL;
let astroMiddlewareEntryPoint: URL | undefined = undefined;
// Extra files to be merged with `includeFiles` during build
const extraFilesToInclude: URL[] = [];
// Secret used to verify that the caller is the astro-generated edge middleware and not a third-party
const middlewareSecret = randomUUID();
let finalBuildOutput: HookParameters<'astro:config:done'>['buildOutput'];
const TRACE_CACHE = {};
const ssrBuildDir = () => new URL('./.netlify/build/', rootDir);
const ssrOutputDir = () => new URL('./.netlify/v1/functions/ssr/', rootDir);
const middlewareOutputDir = () => new URL('.netlify/v1/edge-functions/middleware/', rootDir);
const cleanFunctions = async () =>
await Promise.all([
emptyDir(middlewareOutputDir()),
emptyDir(ssrOutputDir()),
emptyDir(ssrBuildDir()),
]);
async function writeRedirects(
routes: IntegrationResolvedRoute[],
dir: URL,
buildOutput: HookParameters<'astro:config:done'>['buildOutput'],
assets: HookParameters<'astro:build:done'>['assets'],
) {
// all other routes are handled by SSR
const staticRedirects = routes.filter(
(route) => route.type === 'redirect' && (route.redirect || route.redirectRoute),
);
// this is needed to support redirects to dynamic routes
// on static. not sure why this is needed, but it works.
for (const { pattern, redirectRoute } of staticRedirects) {
const distURL = assets.get(pattern);
if (!distURL && redirectRoute) {
const redirectDistURL = assets.get(redirectRoute.pattern);
if (redirectDistURL) {
assets.set(pattern, redirectDistURL);
}
}
}
const fallback = finalBuildOutput === 'static' ? '/.netlify/static' : '/.netlify/functions/ssr';
const redirects = createRedirectsFromAstroRoutes({
config: _config,
dir,
routeToDynamicTargetMap: new Map(staticRedirects.map((route) => [route, fallback])),
buildOutput,
assets,
});
if (!redirects.empty()) {
await appendFile(new URL('_redirects', outDir), `\n${redirects.print()}\n`);
}
}
async function getFilesByGlob(
include: Array<string> = [],
exclude: Array<string> = [],
): Promise<Array<URL>> {
const files = await glob(include, {
cwd: fileURLToPath(rootDir),
absolute: true,
ignore: exclude,
});
return files.map((file) => pathToFileURL(file));
}
async function writeSSRFunction({
notFoundContent,
logger,
root,
}: {
notFoundContent?: string;
logger: AstroIntegrationLogger;
root: URL;
}) {
const entry = new URL('./entry.mjs', ssrBuildDir());
const _includeFiles = integrationConfig?.includeFiles || [];
const _excludeFiles = integrationConfig?.excludeFiles || [];
if (finalBuildOutput === 'server') {
// Merge any includes from `vite.assetsInclude
if (_config.vite.assetsInclude) {
const mergeGlobbedIncludes = (globPattern: unknown) => {
if (typeof globPattern === 'string') {
const entries = glob.sync(globPattern).map((p) => pathToFileURL(p));
extraFilesToInclude.push(...entries);
} else if (Array.isArray(globPattern)) {
for (const pattern of globPattern) {
mergeGlobbedIncludes(pattern);
}
}
};
mergeGlobbedIncludes(_config.vite.assetsInclude);
}
}
const includeFiles = (await getFilesByGlob(_includeFiles, _excludeFiles)).concat(
extraFilesToInclude,
);
const excludeFiles = await getFilesByGlob(_excludeFiles);
const { handler } = await copyDependenciesToFunction(
{
entry,
outDir: ssrOutputDir(),
includeFiles: includeFiles,
excludeFiles: excludeFiles,
logger,
root,
},
TRACE_CACHE,
);
await writeFile(
new URL('./ssr.mjs', ssrOutputDir()),
`
import createSSRHandler from './${handler}';
export default createSSRHandler(${JSON.stringify({
cacheOnDemandPages: Boolean(integrationConfig?.cacheOnDemandPages),
notFoundContent,
})});
export const config = {
includedFiles: ['**/*'],
name: 'Astro SSR',
nodeBundler: 'none',
generator: '@astrojs/netlify@${packageVersion}',
path: '/*',
preferStatic: true,
};
`,
);
}
async function writeMiddleware(entrypoint: URL) {
await mkdir(middlewareOutputDir(), { recursive: true });
await writeFile(
new URL('./entry.mjs', middlewareOutputDir()),
/* ts */ `
import { onRequest } from "${fileURLToPath(entrypoint).replaceAll('\\', '/')}";
import { createContext, trySerializeLocals } from 'astro/middleware';
export default async (request, context) => {
const ctx = createContext({
request,
params: {},
locals: { netlify: { context } }
});
// https://docs.netlify.com/edge-functions/api/#return-a-rewrite
ctx.rewrite = (target) => {
if(target instanceof Request) {
// We can only mutate headers, so if anything else is different, we need to fetch
// the target URL instead.
if(target.method !== request.method || target.body || target.url.origin !== request.url.origin) {
return fetch(target);
}
// We can't replace the headers object, so we need to delete all headers and set them again
request.headers.forEach((_value, key) => {
request.headers.delete(key);
});
target.headers.forEach((value, key) => {
request.headers.set(key, value);
});
return new URL(target.url);
}
return new URL(target, request.url);
};
const next = () => {
const { netlify, ...otherLocals } = ctx.locals;
request.headers.set("x-astro-locals", trySerializeLocals(otherLocals));
request.headers.set("x-astro-middleware-secret", "${middlewareSecret}");
return context.next();
};
return onRequest(ctx, next);
}
export const config = {
name: "Astro Middleware",
generator: "@astrojs/netlify@${packageVersion}",
path: "/*", excludedPath: ["/_astro/*", "/.netlify/images/*"]
};
`,
);
// taking over bundling, because Netlify bundling trips over NPM modules
await build({
entryPoints: [fileURLToPath(new URL('./entry.mjs', middlewareOutputDir()))],
// allow `node:` prefixed imports, which are valid in netlify's deno edge runtime
plugins: [
{
name: 'allowNodePrefixedImports',
setup(puglinBuild) {
puglinBuild.onResolve({ filter: /^node:.*$/ }, (args) => ({
path: args.path,
external: true,
}));
},
},
],
target: 'es2022',
platform: 'neutral',
mainFields: ['module', 'main'],
outfile: fileURLToPath(new URL('./middleware.mjs', middlewareOutputDir())),
allowOverwrite: true,
format: 'esm',
bundle: true,
minify: false,
external: ['sharp'],
banner: {
// Import Deno polyfill for `process.env` at the top of the file
js: 'import process from "node:process";',
},
});
}
function getLocalDevNetlifyContext(req: IncomingMessage): Context {
const isHttps = req.headers['x-forwarded-proto'] === 'https';
const parseBase64JSON = <T = unknown>(header: string): T | undefined => {
if (typeof req.headers[header] === 'string') {
try {
return JSON.parse(Buffer.from(req.headers[header] as string, 'base64').toString('utf8'));
} catch {}
}
};
const context: Context = {
account: parseBase64JSON('x-nf-account-info') ?? {
id: 'mock-netlify-account-id',
},
// TODO: this has type conflicts with @netlify/functions ^2.8.1
// @ts-expect-error: this has type conflicts with @netlify/functions ^2.8.1
deploy: {
id:
typeof req.headers['x-nf-deploy-id'] === 'string'
? req.headers['x-nf-deploy-id']
: 'mock-netlify-deploy-id',
},
site: parseBase64JSON('x-nf-site-info') ?? {
id: 'mock-netlify-site-id',
name: 'mock-netlify-site.netlify.app',
url: `${isHttps ? 'https' : 'http'}://localhost:${isRunningInNetlify ? 8888 : 4321}`,
},
geo: parseBase64JSON('x-nf-geo') ?? {
city: 'Mock City',
country: { code: 'mock', name: 'Mock Country' },
subdivision: { code: 'SD', name: 'Mock Subdivision' },
timezone: 'UTC',
longitude: 0,
latitude: 0,
},
ip:
typeof req.headers['x-nf-client-connection-ip'] === 'string'
? req.headers['x-nf-client-connection-ip']
: (req.socket.remoteAddress ?? '127.0.0.1'),
server: {
region: 'local-dev',
},
requestId:
typeof req.headers['x-nf-request-id'] === 'string'
? req.headers['x-nf-request-id']
: 'mock-netlify-request-id',
get cookies(): never {
throw new Error('Please use Astro.cookies instead.');
},
// @ts-expect-error This is not currently included in the public Netlify types
flags: undefined,
json: (input) => Response.json(input),
log: console.info,
next: () => {
throw new Error('`context.next` is not implemented for serverless functions');
},
get params(): never {
throw new Error("context.params don't contain any usable content in Astro.");
},
rewrite() {
throw new Error('context.rewrite is not available in Astro.');
},
};
return context;
}
let routes: IntegrationResolvedRoute[];
return {
name: '@astrojs/netlify',
hooks: {
'astro:config:setup': async ({ config, updateConfig, logger }) => {
rootDir = config.root;
await cleanFunctions();
outDir = new URL('./dist/', rootDir);
const enableImageCDN = isRunningInNetlify && (integrationConfig?.imageCDN ?? true);
let session = config.session;
if (config.experimental.session && !session?.driver) {
logger.info(
`Configuring experimental session support using ${isRunningInNetlify ? 'Netlify Blobs' : 'filesystem storage'}`,
);
session = isRunningInNetlify
? {
...session,
driver: 'netlify-blobs',
options: {
name: 'astro-sessions',
consistency: 'strong',
...session?.options,
},
}
: {
...session,
driver: 'fs-lite',
options: {
base: fileURLToPath(new URL('sessions', config.cacheDir)),
...session?.options,
},
};
}
updateConfig({
outDir,
build: {
redirects: false,
client: outDir,
server: ssrBuildDir(),
},
session,
vite: {
server: {
watch: {
ignored: [fileURLToPath(new URL('./.netlify/**', rootDir))],
},
},
},
image: {
service: {
entrypoint: enableImageCDN ? '@astrojs/netlify/image-service.js' : undefined,
},
},
});
},
'astro:routes:resolved': (params) => {
routes = params.routes;
},
'astro:config:done': async ({ config, setAdapter, logger, buildOutput }) => {
rootDir = config.root;
_config = config;
finalBuildOutput = buildOutput;
await writeNetlifyFrameworkConfig(config, logger);
const edgeMiddleware = integrationConfig?.edgeMiddleware ?? false;
setAdapter({
name: '@astrojs/netlify',
serverEntrypoint: '@astrojs/netlify/ssr-function.js',
exports: ['default'],
adapterFeatures: {
edgeMiddleware,
},
args: { middlewareSecret } satisfies Args,
supportedAstroFeatures: {
hybridOutput: 'stable',
staticOutput: 'stable',
serverOutput: 'stable',
sharpImageService: 'stable',
envGetSecret: 'stable',
},
});
},
'astro:build:ssr': async ({ middlewareEntryPoint }) => {
astroMiddlewareEntryPoint = middlewareEntryPoint;
},
'astro:build:done': async ({ assets, dir, logger }) => {
await writeRedirects(routes, dir, finalBuildOutput, assets);
logger.info('Emitted _redirects');
if (finalBuildOutput !== 'static') {
let notFoundContent = undefined;
try {
notFoundContent = await readFile(new URL('./404.html', dir), 'utf8');
} catch {}
await writeSSRFunction({ notFoundContent, logger, root: _config.root });
logger.info('Generated SSR Function');
}
if (astroMiddlewareEntryPoint) {
await writeMiddleware(astroMiddlewareEntryPoint);
logger.info('Generated Middleware Edge Function');
}
},
// local dev
'astro:server:setup': async ({ server }) => {
server.middlewares.use((req, _res, next) => {
const locals = Symbol.for('astro.locals');
Reflect.set(req, locals, {
...Reflect.get(req, locals),
netlify: { context: getLocalDevNetlifyContext(req) },
});
next();
});
},
},
};
}
|