Skip to content
Closed
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/modern-now-configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'serve': patch
---

Ignore now.json without legacy static-serving configuration instead of crashing at startup.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Now you understand how the package works! :tada:

## Configuration

A `now.json` file without a legacy `now.static` section is ignored when
looking for static-serving configuration.

To customize `serve`'s behavior, create a `serve.json` file in the public folder and insert any of [these properties](https://github.com/vercel/serve-handler#options).

## API
Expand Down
4 changes: 2 additions & 2 deletions source/utilities/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const loadConfiguration = async (
static?: Partial<Configuration>;
}
interface NowConfigurationFile {
now: { static?: Partial<Configuration> };
now?: { static?: Partial<Configuration> };
}
type ParseableConfiguration =
| ServeConfigurationFile
Expand All @@ -87,7 +87,7 @@ export const loadConfiguration = async (
// Check if any of these files have a serve specific section.
if (file === 'now.json') {
parsedJson = parsedJson as NowConfigurationFile;
parsedJson = parsedJson.now.static;
parsedJson = parsedJson.now?.static;
} else if (file === 'package.json') {
parsedJson = parsedJson as ManifestConfigurationFile;
parsedJson = parsedJson.static;
Expand Down
51 changes: 51 additions & 0 deletions tests/config-legacy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { expect, test } from 'vitest';
import { loadConfiguration } from '../source/utilities/config.js';

test('a modern now.json without a legacy static section does not break static serving', async () => {
const directory = await mkdtemp(join(tmpdir(), 'serve-now-'));
try {
await writeFile(
join(directory, 'now.json'),
JSON.stringify({ version: 2 }),
);
const config = await loadConfiguration(directory, directory, {});
expect(config.public).toBe('');
expect(config.etag).toBe(true);
} finally {
await rm(directory, { recursive: true, force: true });
}
});
test('a legacy now.static section is still loaded', async () => {
const directory = await mkdtemp(join(tmpdir(), 'serve-now-'));
try {
await writeFile(
join(directory, 'now.json'),
JSON.stringify({ now: { static: { cleanUrls: false } } }),
);
const config = await loadConfiguration(directory, directory, {});
expect(config.cleanUrls).toBe(false);
} finally {
await rm(directory, { recursive: true, force: true });
}
});

test('a modern now.json falls through to package.json static settings', async () => {
const directory = await mkdtemp(join(tmpdir(), 'serve-now-'));
try {
await writeFile(
join(directory, 'now.json'),
JSON.stringify({ version: 2 }),
);
await writeFile(
join(directory, 'package.json'),
JSON.stringify({ static: { cleanUrls: false } }),
);
const config = await loadConfiguration(directory, directory, {});
expect(config.cleanUrls).toBe(false);
} finally {
await rm(directory, { recursive: true, force: true });
}
});