Skip to content
Open
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: 5 additions & 0 deletions .changeset/calm-bats-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': patch
---

fix: omit ISR data endpoints for server-only routes
19 changes: 13 additions & 6 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ const plugin = function (defaults = {}) {
if (isr) {
const isr_name = route.id.slice(1) || '__root__'; // should we check that __root__ isn't a route?
const base = `${dirs.functions}/${isr_name}`;
const has_page = route.page.methods.length > 0;
fs.mkdirSync(base, { recursive: true });

const target = `${dirs.functions}/${name}.func`;
Expand All @@ -246,7 +247,9 @@ const plugin = function (defaults = {}) {
// create a symlink to the actual function, but use the
// route name so that we can derive the correct URL
fs.symlinkSync(relative, `${base}.func`);
fs.symlinkSync(`../${relative}`, `${base}/__data.json.func`);
if (has_page) {
fs.symlinkSync(`../${relative}`, `${base}/__data.json.func`);
}

const pathname = get_pathname(route);
const json = JSON.stringify(
Expand All @@ -256,7 +259,9 @@ const plugin = function (defaults = {}) {
);

write(`${base}.prerender-config.json`, json);
write(`${base}/__data.json.prerender-config.json`, json);
if (has_page) {
write(`${base}/__data.json.prerender-config.json`, json);
}

const q = `?__pathname=/${pathname}`;

Expand All @@ -265,10 +270,12 @@ const plugin = function (defaults = {}) {
dest: `/${isr_name}${q}`
});

static_config.routes.push({
src: src + '/__data.json$',
dest: `/${isr_name}/__data.json${q}`
});
if (has_page) {
static_config.routes.push({
src: src + '/__data.json$',
dest: `/${isr_name}/__data.json${q}`
});
}
} else {
// Create a symlink for each route to the main function for better observability
// (without this, every request appears to go through `/![-]`)
Expand Down
45 changes: 45 additions & 0 deletions packages/adapter-vercel/test/apps/basic/assert-build-output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';

const output = '.vercel/output';
const functions = `${output}/functions/api/json`;

assert(fs.existsSync(`${functions}.func`), 'expected the API route function to be generated');
assert(
fs.existsSync(`${functions}.prerender-config.json`),
'expected the API route ISR configuration to be generated'
);

assert(
!fs.existsSync(`${functions}/__data.json.func`),
'did not expect a data function for a server-only route'
);
assert(
!fs.existsSync(`${functions}/__data.json.prerender-config.json`),
'did not expect data ISR configuration for a server-only route'
);

/** @type {{ routes: Array<{ src?: string }> }} */
const config = JSON.parse(fs.readFileSync(`${output}/config.json`, 'utf8'));
const route_sources = config.routes.flatMap((route) =>
typeof route.src === 'string' ? [route.src] : []
);
const api_route_sources = route_sources.filter((src) => src.includes('/api/json'));
const isr_page_route_sources = route_sources.filter((src) => src.includes('/isr'));
assert(
api_route_sources.some((src) => !src.includes('__data.json')),
'expected the API route to be present in the Vercel routing configuration'
);
assert(
!api_route_sources.some((src) => src.includes('__data.json')),
'did not expect a data endpoint for a server-only route'
);

assert(
fs.existsSync(`${output}/functions/isr/__data.json.func`),
'expected a data function for an ISR page route'
);
assert(
isr_page_route_sources.some((src) => src.includes('__data.json')),
'expected the ISR page data endpoint to remain in the Vercel routing configuration'
);
2 changes: 1 addition & 1 deletion packages/adapter-vercel/test/apps/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"dev": "vite dev",
"preview": "vite preview",
"build": "vite build",
"build": "vite build && node assert-build-output.js",
"test:platform": "playwright test"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { json } from '@sveltejs/kit';

export const config = {
isr: {
expiration: 60
}
};

export function GET() {
return json({ ok: true });
}
Loading