vite-plugin-vercel 10.0.0-beta.4 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,11 +1,6 @@
1
1
  # vite-plugin-vercel
2
2
 
3
- > [!WARNING]
4
- > :construction: Work In Progress
5
- >
6
- > You are on the [Vite Environment API](https://vite.dev/guide/api-environment.html#environment-configuration) development branch. Check out [v9 branch](https://github.com/magne4000/vite-plugin-vercel/tree/v9) current stable version.
7
-
8
- Vercel adapter for [Vite 6](https://vitejs.dev/).
3
+ Vercel adapter for [Vite](https://vitejs.dev/).
9
4
 
10
5
  Bundle your Vite application as supported by [Vercel Output API (v3)](https://vercel.com/docs/build-output-api/v3).
11
6
 
@@ -50,32 +45,27 @@ Install this package as a dev dependency and add it to your Vite config:
50
45
  // vite.config.ts
51
46
  import { defineConfig } from 'vite';
52
47
  import vercel from 'vite-plugin-vercel';
53
- import { getEntriesFromFs } from "vite-plugin-vercel/utils";
54
48
 
55
49
  export default defineConfig({
56
- plugins: [vercel({
57
- entries: [
58
- ...(await getEntriesFromFs("endpoints/api", {
59
- // Auto mapping examples:
60
- // endpoints/api/page.ts -> /api/page
61
- // endpoints/api/name/[name].ts -> /api/name/*
62
- destination: "api",
63
- }))
64
- ]
65
- })],
50
+ plugins: [vercel()],
51
+ vercel: {
52
+ // optional configuration options, see "Advanced usage" below for details
53
+ },
66
54
  });
67
55
  ```
68
56
 
69
57
  > [!NOTE]
70
- > `@vercel/build` currently forces the building of files in the _/api_ folder, with no way to disable this behavior.
71
- > It's recommended to place your files in a different folder.
58
+ > Files under `/api` or `/_api` directory will automatically be added under `/api/*` route
59
+ > Prefer using `/_api` directory, as `@vercel/build` is currently force building `/api` files,
60
+ > with no way to disable it, thus avoiding double compilation and unexpected behaviour.
72
61
 
73
62
  ### Configure endpoints
74
63
 
75
- Endpoints added via `getEntriesFromFs` can be configured by exporting values from the endpoint file:
64
+ Endpoints under `/api`, `/_api` or added through `additionalEndpoints` can be configured
65
+ by exporting values from the endpoint file:
76
66
 
77
67
  ```ts
78
- // file: endpoints/api/endpoint.ts
68
+ // file: _api/endpoint.ts
79
69
 
80
70
  // Should run on edge runtime
81
71
  export const edge = true;
@@ -100,23 +90,170 @@ export default async function handler() {
100
90
  }
101
91
  ```
102
92
 
93
+ > [!NOTE]
94
+ > Please create an issue if you need other per-endpoints configurations
95
+
103
96
  ### Edge middleware
104
97
 
105
98
  You can use [Edge middleware as describe in the official documentation](https://vercel.com/docs/functions/edge-middleware/middleware-api) (i.e. with a `middleware.ts` file at the root of your project).
106
99
 
107
- ## FAQ
100
+ ## Usage with Vike
108
101
 
109
- ### What does ISR do in dev mode?
110
- Nothing. It's a production-only feature
102
+ [Vike](https://vike.dev/) is supported through [@vite-plugin-vercel/vike](/packages/vike-integration/README.md) plugin.
111
103
 
112
- ### What does `edge: true` target do in dev mode?
113
- Nothing (yet?). If you have a use-case where an actual Edge runtime would be necessary in dev, please open a discussion
104
+ You only need to install `@vite-plugin-vercel/vike`, the Vite config stays the same as above.
114
105
 
115
- ### I don't see Vercel specific headers in dev mode
116
- This is not yet supported. Please open an issue if you need this (PR welcome).
106
+ You can then leverage [config files](https://vike.dev/config) to customize your endpoints:
107
+
108
+ ```ts
109
+ // /pages/product/+config.ts
110
+
111
+ import Page from './Page';
112
+ import type { Config } from 'vike/types';
113
+
114
+ export default {
115
+ // Customize ISR config for this page
116
+ isr: { expiration: 15 },
117
+ // Target Edge instead of Serverless
118
+ edge: true,
119
+ // append headers to all responses
120
+ headers: {
121
+ 'X-Header': 'value'
122
+ }
123
+ } satisfies Config;
124
+ ```
117
125
 
118
- Related documentation: https://vercel.com/docs/edge-network/headers/request-headers
126
+ You will also need to extend the [renderer config](https://vike.dev/config#renderer) so that `vike` is aware of the new parameter:
127
+
128
+ ```ts
129
+ // /renderer/+config.ts
130
+
131
+ import config from '@vite-plugin-vercel/vike/config';
132
+ import type { Config } from 'vike/types';
133
+
134
+ export default {
135
+ extends: config,
136
+ } satisfies Config;
137
+ ```
138
+
139
+ ## Advanced usage
140
+
141
+ ```ts
142
+ // vite.config.ts
143
+ import { defineConfig } from 'vite';
144
+ import vercel from 'vite-plugin-vercel';
145
+
146
+ export default defineConfig({
147
+ plugins: [vercel()],
148
+ vercel: {
149
+ // All the followings optional
150
+
151
+ /**
152
+ * How long Functions should be allowed to run for every request, in seconds.
153
+ * If left empty, default value for your plan will be used.
154
+ */
155
+ defaultMaxDuration: 30,
156
+ /**
157
+ * Enable streaming responses by default for all Serverless Functions
158
+ */
159
+ defaultSupportsResponseStreaming: true,
160
+ /**
161
+ * Default expiration time (in seconds) for prerender functions.
162
+ * Defaults to 86400 seconds (24h).
163
+ */
164
+ expiration: 86400,
165
+ /**
166
+ * Also known as Server Side Generation, or SSG.
167
+ * If present, this function is responsible to create static files in `.vercel/output/static`.
168
+ * Defaults to `false`, which disables prerendering.
169
+ */
170
+ prerender(resolvedConfig) {
171
+ // Check `/packages/vike/vike.ts` `prerender` for an example
172
+ },
173
+ /**
174
+ * See https://vercel.com/docs/projects/project-configuration#rewrites
175
+ */
176
+ rewrites: [{ source: '/about', destination: '/about-our-company.html' }],
177
+ /**
178
+ * @see {@link https://vercel.com/docs/projects/project-configuration#headers}
179
+ */
180
+ headers: [
181
+ {
182
+ "source": "/service-worker.js",
183
+ "headers": [
184
+ {
185
+ "key": "Cache-Control",
186
+ "value": "public, max-age=0, must-revalidate"
187
+ }
188
+ ]
189
+ }
190
+ ],
191
+ /**
192
+ * See https://vercel.com/docs/projects/project-configuration#redirects
193
+ */
194
+ redirects: [
195
+ { source: '/me', destination: '/profile.html', permanent: false },
196
+ ],
197
+ /**
198
+ * See https://vercel.com/docs/projects/project-configuration#cleanurls
199
+ */
200
+ cleanUrls: true,
201
+ /**
202
+ * See https://vercel.com/docs/projects/project-configuration#trailingslash
203
+ */
204
+ trailingSlash: true,
205
+ /**
206
+ * By default, all `api/*` and `_api/*` endpoints are compiled under `.vercel/output/functions/api/*.func`.
207
+ * If others serverless functions need to be compiled under `.vercel/output/functions`, they should be added here.
208
+ * For instance, a framework can leverage this to have a generic ssr endpoint
209
+ * without requiring the user to write any code.
210
+ */
211
+ additionalEndpoints: [
212
+ {
213
+ // can also be an Object representing an esbuild StdinOptions
214
+ source: '/path/to/file.ts',
215
+ // URL path of the handler, will be generated to `.vercel/output/functions/api/file.func/index.js`
216
+ destination: '/api/file',
217
+ },
218
+ ],
219
+ /**
220
+ * Advanced configuration to override .vercel/output/config.json
221
+ * See https://vercel.com/docs/build-output-api/v3/configuration#configuration
222
+ */
223
+ config: {
224
+ // routes?: Route[];
225
+ // images?: ImagesConfig;
226
+ // wildcard?: WildcardConfig;
227
+ // overrides?: OverrideConfig;
228
+ // cache?: string[];
229
+ // crons?: CronsConfig;
230
+ },
231
+ /**
232
+ * ISR and SSG pages are mutually exclusive. If a page is found in both, ISR prevails.
233
+ * Keys are path relative to .vercel/output/functions directory, either without extension,
234
+ * or with `.prerender-config.json` extension.
235
+ * If you have multiple isr configurations pointing to the same underlying function, you can leverage the `symlink`
236
+ * property.
237
+ *
238
+ * Can be an object or a function returning an object (or a Promise of an object).
239
+ *
240
+ * Check `/packages/vike/vike.ts` `vitePluginVercelVpsIsrPlugin` for advanced usage.
241
+ */
242
+ isr: {
243
+ // `symlink: 'ssr_'` means that a function is available under `.vercel/output/functions/ssr_.func`
244
+ '/pages/a': { expiration: 15, symlink: 'ssr_', route: '^/a/.*$' },
245
+ '/pages/b/c': { expiration: 15, symlink: 'ssr_', route: '^/b/c/.*$' },
246
+ '/pages/d': { expiration: 15, symlink: 'ssr_', route: '^/d$' },
247
+ '/pages/e': { expiration: 25 },
248
+ },
249
+ /**
250
+ * Defaults to `.vercel/output`. Mostly useful for testing purpose
251
+ */
252
+ outDir: '.vercel/output',
253
+ },
254
+ });
255
+ ```
119
256
 
120
- ## Migration from v9
257
+ ## Demo
121
258
 
122
- TODO
259
+ https://vike-photon-demo.vercel.app/