mirror of
https://github.com/withastro/astro.git
synced 2025-02-03 22:29:08 -05:00
[ci] format
This commit is contained in:
parent
9e5fafa2b2
commit
c490eb6e52
7 changed files with 33 additions and 32 deletions
|
@ -1,9 +1,9 @@
|
||||||
import type { AstroConfig, AstroSettings, ManifestData, RuntimeMode } from '../../@types/astro';
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import * as colors from 'kleur/colors';
|
import * as colors from 'kleur/colors';
|
||||||
import { performance } from 'perf_hooks';
|
import { performance } from 'perf_hooks';
|
||||||
import type * as vite from 'vite';
|
import type * as vite from 'vite';
|
||||||
import type yargs from 'yargs-parser';
|
import type yargs from 'yargs-parser';
|
||||||
|
import type { AstroConfig, AstroSettings, ManifestData, RuntimeMode } from '../../@types/astro';
|
||||||
import {
|
import {
|
||||||
runHookBuildDone,
|
runHookBuildDone,
|
||||||
runHookBuildStart,
|
runHookBuildStart,
|
||||||
|
@ -11,7 +11,7 @@ import {
|
||||||
runHookConfigSetup,
|
runHookConfigSetup,
|
||||||
} from '../../integrations/index.js';
|
} from '../../integrations/index.js';
|
||||||
import { createVite } from '../create-vite.js';
|
import { createVite } from '../create-vite.js';
|
||||||
import { debug, info, warn, levels, timerMessage, type LogOptions } from '../logger/core.js';
|
import { debug, info, levels, timerMessage, warn, type LogOptions } from '../logger/core.js';
|
||||||
import { printHelp } from '../messages.js';
|
import { printHelp } from '../messages.js';
|
||||||
import { apply as applyPolyfill } from '../polyfill.js';
|
import { apply as applyPolyfill } from '../polyfill.js';
|
||||||
import { RouteCache } from '../render/route-cache.js';
|
import { RouteCache } from '../render/route-cache.js';
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import type { Plugin as VitePlugin } from 'vite';
|
import type { Plugin as VitePlugin } from 'vite';
|
||||||
import { MIDDLEWARE_PATH_SEGMENT_NAME } from '../../constants.js';
|
import { MIDDLEWARE_PATH_SEGMENT_NAME } from '../../constants.js';
|
||||||
import { addRollupInput } from '../add-rollup-input.js';
|
import { addRollupInput } from '../add-rollup-input.js';
|
||||||
|
import type { BuildInternals } from '../internal';
|
||||||
import type { AstroBuildPlugin } from '../plugin';
|
import type { AstroBuildPlugin } from '../plugin';
|
||||||
import type { StaticBuildOptions } from '../types';
|
import type { StaticBuildOptions } from '../types';
|
||||||
import type { BuildInternals } from '../internal';
|
|
||||||
|
|
||||||
export const MIDDLEWARE_MODULE_ID = '@astro-middleware';
|
export const MIDDLEWARE_MODULE_ID = '@astro-middleware';
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import type { MiddlewareResponseHandler, Params } from '../../@types/astro';
|
import type { MiddlewareResponseHandler, Params } from '../../@types/astro';
|
||||||
import { sequence } from './sequence.js';
|
|
||||||
import { createAPIContext } from '../endpoint/index.js';
|
import { createAPIContext } from '../endpoint/index.js';
|
||||||
|
import { sequence } from './sequence.js';
|
||||||
|
|
||||||
function defineMiddleware(fn: MiddlewareResponseHandler) {
|
function defineMiddleware(fn: MiddlewareResponseHandler) {
|
||||||
return fn;
|
return fn;
|
||||||
|
|
|
@ -270,15 +270,15 @@ This is an opt-in feature, and the `build.excludeMiddleware` option needs to be
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// astro.config.mjs
|
// astro.config.mjs
|
||||||
import {defineConfig} from "astro/config";
|
import { defineConfig } from 'astro/config';
|
||||||
import vercel from "@astrojs/vercel";
|
import vercel from '@astrojs/vercel';
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
output: "server",
|
output: 'server',
|
||||||
adapter: vercel(),
|
adapter: vercel(),
|
||||||
build: {
|
build: {
|
||||||
excludeMiddleware: true
|
excludeMiddleware: true,
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
Optionally, you can create a file recognized by the adapter named `vercel-edge-middleware.(js|ts)` in the [`srcDir`](https://docs.astro.build/en/reference/configuration-reference/#srcdir) folder to create [`Astro.locals`](https://docs.astro.build/en/reference/api-reference/#astrolocals).
|
Optionally, you can create a file recognized by the adapter named `vercel-edge-middleware.(js|ts)` in the [`srcDir`](https://docs.astro.build/en/reference/configuration-reference/#srcdir) folder to create [`Astro.locals`](https://docs.astro.build/en/reference/api-reference/#astrolocals).
|
||||||
|
@ -293,11 +293,11 @@ Typings requires the [`@vercel/edge`](https://www.npmjs.com/package/@vercel/edge
|
||||||
* @param options.context {import("@vercel/edge").RequestContext}
|
* @param options.context {import("@vercel/edge").RequestContext}
|
||||||
* @returns {object}
|
* @returns {object}
|
||||||
*/
|
*/
|
||||||
export default function({ request, context }) {
|
export default function ({ request, context }) {
|
||||||
// do something with request and context
|
// do something with request and context
|
||||||
return {
|
return {
|
||||||
title: "Spider-man's blog"
|
title: "Spider-man's blog",
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -305,19 +305,20 @@ If you use TypeScript, you can type the function as follows:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
// src/vercel-edge-middleware.ts
|
// src/vercel-edge-middleware.ts
|
||||||
import type {RequestContext} from "@vercel/edge";
|
import type { RequestContext } from '@vercel/edge';
|
||||||
|
|
||||||
export default function ({request, context}: { request: Request, context: RequestContext }) {
|
export default function ({ request, context }: { request: Request; context: RequestContext }) {
|
||||||
// do something with request and context
|
// do something with request and context
|
||||||
return {
|
return {
|
||||||
title: "Spider-man's blog"
|
title: "Spider-man's blog",
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The data returned by this function will be passed to Astro middleware.
|
The data returned by this function will be passed to Astro middleware.
|
||||||
|
|
||||||
The function:
|
The function:
|
||||||
|
|
||||||
- must export a **default** function;
|
- must export a **default** function;
|
||||||
- must **return** an `object`;
|
- must **return** an `object`;
|
||||||
- accepts an object with a `request` and `context` as properties;
|
- accepts an object with a `request` and `context` as properties;
|
||||||
|
@ -327,11 +328,11 @@ The function:
|
||||||
#### Limitations and constraints
|
#### Limitations and constraints
|
||||||
|
|
||||||
When you opt in to this feature, there are few constraints to note:
|
When you opt in to this feature, there are few constraints to note:
|
||||||
|
|
||||||
- The Vercel Edge middleware will always be the **first** function to receive the `Request` and the last function to receive `Response`. This an architectural constraint that follows the [boundaries set by Vercel](https://vercel.com/docs/concepts/functions/edge-middleware).
|
- The Vercel Edge middleware will always be the **first** function to receive the `Request` and the last function to receive `Response`. This an architectural constraint that follows the [boundaries set by Vercel](https://vercel.com/docs/concepts/functions/edge-middleware).
|
||||||
- Only `request` and `context` may be used to produce an `Astro.locals` object. Operations like redirects, etc. should be delegated to Astro middleware.
|
- Only `request` and `context` may be used to produce an `Astro.locals` object. Operations like redirects, etc. should be delegated to Astro middleware.
|
||||||
- `Astro.locals` **must be serializable**. Failing to do so will result in a **runtime error**. This means that you **cannot** store complex types like `Map`, `function`, `Set`, etc.
|
- `Astro.locals` **must be serializable**. Failing to do so will result in a **runtime error**. This means that you **cannot** store complex types like `Map`, `function`, `Set`, etc.
|
||||||
|
|
||||||
|
|
||||||
## Troubleshooting
|
## Troubleshooting
|
||||||
|
|
||||||
**A few known complex packages (example: [puppeteer](https://github.com/puppeteer/puppeteer)) do not support bundling and therefore will not work properly with this adapter.** By default, Vercel doesn't include npm installed files & packages from your project's `./node_modules` folder. To address this, the `@astrojs/vercel` adapter automatically bundles your final build output using `esbuild`.
|
**A few known complex packages (example: [puppeteer](https://github.com/puppeteer/puppeteer)) do not support bundling and therefore will not work properly with this adapter.** By default, Vercel doesn't include npm installed files & packages from your project's `./node_modules` folder. To address this, the `@astrojs/vercel` adapter automatically bundles your final build output using `esbuild`.
|
||||||
|
|
|
@ -2,6 +2,7 @@ import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'ast
|
||||||
|
|
||||||
import glob from 'fast-glob';
|
import glob from 'fast-glob';
|
||||||
import { basename } from 'node:path';
|
import { basename } from 'node:path';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
import { pathToFileURL } from 'url';
|
import { pathToFileURL } from 'url';
|
||||||
import {
|
import {
|
||||||
defaultImageConfig,
|
defaultImageConfig,
|
||||||
|
@ -14,7 +15,6 @@ import { getVercelOutput, removeDir, writeJson } from '../lib/fs.js';
|
||||||
import { copyDependenciesToFunction } from '../lib/nft.js';
|
import { copyDependenciesToFunction } from '../lib/nft.js';
|
||||||
import { getRedirects } from '../lib/redirects.js';
|
import { getRedirects } from '../lib/redirects.js';
|
||||||
import { generateEdgeMiddleware } from './middleware.js';
|
import { generateEdgeMiddleware } from './middleware.js';
|
||||||
import { fileURLToPath } from 'node:url';
|
|
||||||
|
|
||||||
const PACKAGE_NAME = '@astrojs/vercel/serverless';
|
const PACKAGE_NAME = '@astrojs/vercel/serverless';
|
||||||
export const ASTRO_LOCALS_HEADER = 'x-astro-locals';
|
export const ASTRO_LOCALS_HEADER = 'x-astro-locals';
|
||||||
|
|
|
@ -3,8 +3,8 @@ import type { SSRManifest } from 'astro';
|
||||||
import { App } from 'astro/app';
|
import { App } from 'astro/app';
|
||||||
import type { IncomingMessage, ServerResponse } from 'node:http';
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
||||||
|
|
||||||
import { getRequest, setResponse } from './request-transform';
|
|
||||||
import { ASTRO_LOCALS_HEADER } from './adapter';
|
import { ASTRO_LOCALS_HEADER } from './adapter';
|
||||||
|
import { getRequest, setResponse } from './request-transform';
|
||||||
|
|
||||||
polyfill(globalThis, {
|
polyfill(globalThis, {
|
||||||
exclude: 'window document',
|
exclude: 'window document',
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
||||||
import { join } from 'node:path';
|
|
||||||
import { ASTRO_LOCALS_HEADER } from './adapter.js';
|
|
||||||
import { existsSync } from 'fs';
|
import { existsSync } from 'fs';
|
||||||
|
import { join } from 'node:path';
|
||||||
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||||
|
import { ASTRO_LOCALS_HEADER } from './adapter.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* It generates the Vercel Edge Middleware file.
|
* It generates the Vercel Edge Middleware file.
|
||||||
|
|
Loading…
Add table
Reference in a new issue