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

Revert "feat: include server assets for Vercel serverless functions" #11644

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .changeset/loud-needles-refuse.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/selfish-bobcats-run.md

This file was deleted.

27 changes: 3 additions & 24 deletions documentation/docs/25-build-and-deploy/80-adapter-netlify.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,8 @@ Additionally, you can add your own Netlify functions by creating a directory for
directory = "functions"
```

### Accessing the file system

You can [use files in Netlify Serverless Functions](https://www.netlify.com/blog/2021/08/12/how-to-include-files-in-netlify-serverless-functions/).

```js
// @errors: 2307 7031
/// file: +server.js
import fs from "node:fs";
import path from "node:path";
import { dev } from '$app/environment';
## Troubleshooting

// importing a static asset will return the resolved path in the production build
import PalatinoBoldFont from "$lib/fonts/PalatinoBold.ttf";

const cwd = process.cwd();

// server assets live in `.netlify/server` when deployed to Netlify
const dir = dev ? cwd : path.join(cwd, '.netlify/server');

const pathToFile = path.join(dir, PalatinoBoldFont);
### Accessing the file system

export async function GET() {
const file = fs.readFileSync(pathToFile);
// ...
}
```
You can't access the file system through methods like `fs.readFileSync` in Serverless/Edge environments. If you need to access files that way, do that during building the app through [prerendering](https://kit.svelte.dev/docs/page-options#prerender). If you have a blog for example and don't want to manage your content through a CMS, then you need to prerender the content (or prerender the endpoint from which you get it) and redeploy your blog everytime you add new content.
23 changes: 3 additions & 20 deletions documentation/docs/25-build-and-deploy/90-adapter-vercel.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,8 @@ If you have Vercel functions contained in the `api` directory at the project's r

Projects created before a certain date may default to using an older Node version than what SvelteKit currently requires. You can [change the Node version in your project settings](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/node-js#node.js-version).

### Accessing the file system

You can [use files in Serverless Functions on Vercel](https://vercel.com/guides/how-can-i-use-files-in-serverless-functions).

```js
// @errors: 2307 7031
/// file: api/pdf/+server.js
import fs from "node:fs";
import path from "node:path";

// importing a static asset will return the resolved path in the production build
import PalatinoBoldFont from "$lib/fonts/PalatinoBold.ttf";
## Troubleshooting

const pathToFile = path.join(process.cwd(), PalatinoBoldFont);

export async function GET() {
const file = fs.readFileSync(pathToFile);
// ...
}
```
### Accessing the file system

> Only assets that are imported in `+page.server`, `+layout.server` and `+server` files are included in the Serverless Function bundle.
You can't access the file system through methods like `fs.readFileSync` in Serverless/Edge environments. If you need to access files that way, do that during building the app through [prerendering](https://kit.svelte.dev/docs/page-options#prerender). If you have a blog for example and don't want to manage your content through a CMS, then you need to prerender the content (or prerender the endpoint from which you get it) and redeploy your blog everytime you add new content.
1 change: 0 additions & 1 deletion packages/adapter-vercel/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.DS_Store
node_modules
.vercel
49 changes: 13 additions & 36 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ const plugin = function (defaults = {}) {

/**
* @param {string} name
* @param {import('./index.js').ServerlessConfig} config
* @param {import('@sveltejs/kit').RouteDefinition<import('./index.js').Config>[]} routes
* @param {import('.').ServerlessConfig} config
* @param {import('@sveltejs/kit').RouteDefinition<import('.').Config>[]} routes
*/
async function generate_serverless_function(name, config, routes) {
const relativePath = path.posix.relative(tmp, builder.getServerDirectory());
Expand All @@ -81,15 +81,14 @@ const plugin = function (defaults = {}) {
builder,
`${tmp}/index.js`,
`${dirs.functions}/${name}.func`,
config,
routes
config
);
}

/**
* @param {string} name
* @param {import('./index.js').EdgeConfig} config
* @param {import('@sveltejs/kit').RouteDefinition<import('./index.js').EdgeConfig>[]} routes
* @param {import('.').EdgeConfig} config
* @param {import('@sveltejs/kit').RouteDefinition<import('.').EdgeConfig>[]} routes
*/
async function generate_edge_function(name, config, routes) {
const tmp = builder.getBuildDirectory(`vercel-tmp/${name}`);
Expand Down Expand Up @@ -136,7 +135,7 @@ const plugin = function (defaults = {}) {
);
}

/** @type {Map<string, { i: number, config: import('./index.js').Config, routes: import('@sveltejs/kit').RouteDefinition<import('./index.js').Config>[] }>} */
/** @type {Map<string, { i: number, config: import('.').Config, routes: import('@sveltejs/kit').RouteDefinition<import('.').Config>[] }>} */
const groups = new Map();

/** @type {Map<string, { hash: string, route_id: string }>} */
Expand All @@ -145,7 +144,7 @@ const plugin = function (defaults = {}) {
/** @type {Map<string, string>} */
const functions = new Map();

/** @type {Map<import('@sveltejs/kit').RouteDefinition<import('./index.js').Config>, { expiration: number | false, bypassToken: string | undefined, allowQuery: string[], group: number, passQuery: true }>} */
/** @type {Map<import('@sveltejs/kit').RouteDefinition<import('.').Config>, { expiration: number | false, bypassToken: string | undefined, allowQuery: string[], group: number, passQuery: true }>} */
const isr_config = new Map();

/** @type {Set<string>} */
Expand All @@ -164,7 +163,7 @@ const plugin = function (defaults = {}) {
}

const node_runtime = /nodejs([0-9]+)\.x/.exec(runtime);
if (runtime !== 'edge' && (!node_runtime || +node_runtime[1] < 18)) {
if (runtime !== 'edge' && (!node_runtime || node_runtime[1] < 18)) {
throw new Error(
`Invalid runtime '${runtime}' for route ${route.id}. Valid runtimes are 'edge' and 'nodejs18.x' or higher ` +
'(see the Node.js Version section in your Vercel project settings for info on the currently supported versions).'
Expand Down Expand Up @@ -369,7 +368,7 @@ function write(file, data) {
// This function is duplicated in adapter-static
/**
* @param {import('@sveltejs/kit').Builder} builder
* @param {import('index.js').Config} config
* @param {import('.').Config} config
*/
function static_vercel_config(builder, config) {
/** @type {any[]} */
Expand All @@ -378,11 +377,8 @@ function static_vercel_config(builder, config) {
/** @type {Record<string, { path: string }>} */
const overrides = {};

/** @type {import('./index.js').ImagesConfig | undefined} */
let images;
if (config.runtime !== 'edge') {
images = /** @type {import('./index.js').ServerlessConfig} */ (config).images;
}
/** @type {import('./index').ImagesConfig} */
const images = config.images;

for (const [src, redirect] of builder.prerendered.redirects) {
prerendered_redirects.push({
Expand Down Expand Up @@ -438,10 +434,9 @@ function static_vercel_config(builder, config) {
* @param {import('@sveltejs/kit').Builder} builder
* @param {string} entry
* @param {string} dir
* @param {import('./index.js').ServerlessConfig} config
* @param {import('@sveltejs/kit').RouteDefinition[]} routes
* @param {import('.').ServerlessConfig} config
*/
async function create_function_bundle(builder, entry, dir, config, routes) {
async function create_function_bundle(builder, entry, dir, config) {
fs.rmSync(dir, { force: true, recursive: true });

let base = entry;
Expand Down Expand Up @@ -549,24 +544,6 @@ async function create_function_bundle(builder, entry, dir, config, routes) {
)
);

const server_assets = builder.getServerAssets();
let routes_assets = new Set(server_assets.rootErrorPage);

for (const route of routes) {
const assets = server_assets.routes.get(route.id);
if (assets) {
routes_assets = new Set([...routes_assets, ...assets]);
}
}

if (server_assets.hooks) {
routes_assets = new Set([...routes_assets, ...server_assets.hooks]);
}

for (const asset of routes_assets) {
builder.copy(path.join(builder.getServerDirectory(), asset), path.join(dir, asset));
}

write(`${dir}/package.json`, JSON.stringify({ type: 'module' }));
}

Expand Down
3 changes: 1 addition & 2 deletions packages/adapter-vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@
"lint": "prettier --check .",
"format": "pnpm lint --write",
"check": "tsc",
"test": "vitest run & pnpm -r --workspace-concurrency 1 --filter=\"./test/**\" test"
"test": "vitest run"
},
"dependencies": {
"@vercel/nft": "^0.26.1",
"esbuild": "^0.19.9"
},
"devDependencies": {
"@playwright/test": "1.30.0",
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "^3.0.1",
"@types/node": "^18.19.3",
Expand Down
10 changes: 0 additions & 10 deletions packages/adapter-vercel/test/apps/split/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion packages/adapter-vercel/test/apps/split/.npmrc

This file was deleted.

18 changes: 0 additions & 18 deletions packages/adapter-vercel/test/apps/split/package.json

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions packages/adapter-vercel/test/apps/split/src/app.d.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/adapter-vercel/test/apps/split/src/app.html

This file was deleted.

6 changes: 0 additions & 6 deletions packages/adapter-vercel/test/apps/split/src/hooks.server.js

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Empty file.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions packages/adapter-vercel/test/apps/split/src/lib/transitive.js

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions packages/adapter-vercel/test/apps/split/src/routes/+layout.js

This file was deleted.

This file was deleted.

This file was deleted.

Empty file.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading