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
11 changes: 9 additions & 2 deletions source/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,21 @@ if (configError || !config) {

// If the user wants all the URLs rewritten to `/index.html`, make it happen.
if (args['--single']) {
const singleFile = args['--single-file'];
let destination = '/index.html';

if (singleFile && singleFile.length > 0) {
destination = singleFile.startsWith('/') ? singleFile : `/${singleFile}`;
}

const { rewrites } = config;
const existingRewrites = Array.isArray(rewrites) ? rewrites : [];

// Ensure this is the first rewrite rule so it gets priority.
config.rewrites = [
{
source: '**',
destination: '/index.html',
source: '/**/:path([^/.]+)',
destination,
},
...existingRewrites,
];
Expand Down
1 change: 1 addition & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export declare interface Options {
'--version': boolean;
'--listen': ParsedEndpoint[];
'--single': boolean;
'--single-file': string;
'--debug': boolean;
'--config': Path;
'--no-request-logging': boolean;
Expand Down
3 changes: 3 additions & 0 deletions source/utilities/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const helpText = chalkTemplate`

-s, --single Rewrite all not-found requests to \`index.html\`

--single-file Rewrite all not-found requests to custom page (default: index.html)

-d, --debug Show debugging information

-c, --config Specify custom path to \`serve.json\`
Expand Down Expand Up @@ -148,6 +150,7 @@ const options = {
'--version': Boolean,
'--listen': [parseEndpoint] as [typeof parseEndpoint],
'--single': Boolean,
'--single-file': String,
'--debug': Boolean,
'--config': String,
'--no-clipboard': Boolean,
Expand Down
1 change: 1 addition & 0 deletions tests/__fixtures__/server/app/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('bundle');
2 changes: 2 additions & 0 deletions tests/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ exports[`utilities/cli > render help text 1`] = `

-s, --single Rewrite all not-found requests to \`index.html\`

--single-file Rewrite all not-found requests to custom page (default: index.html)

-d, --debug Show debugging information

-c, --config Specify custom path to \`serve.json\`
Expand Down
17 changes: 17 additions & 0 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getHelpText,
parseEndpoint,
checkForUpdates,
parseArguments,
} from '../source/utilities/cli.js';
import { logger } from '../source/utilities/logger.js';
import { ParsedEndpoint } from '../source/types.js';
Expand Down Expand Up @@ -42,6 +43,22 @@ describe('utilities/cli', () => {
// message, then make sure to run `vitest` with the `--update-snapshot` flag.
test('render help text', () => expect(getHelpText()).toMatchSnapshot());

// Make sure we can set a custom fallback file when using single-page mode.
test('parse single-file option', () => {
const originalArguments = process.argv;

try {
process.argv = ['node', 'test', '-s', '--single-file', '_shell.html'];

const args = parseArguments();

expect(args['--single']).toBe(true);
expect(args['--single-file']).toBe('_shell.html');
} finally {
process.argv = originalArguments;
}
});

// Make sure the `parseEndpoint` function parses valid endpoints correctly.
test.each(validEndpoints)(
'parse %s as endpoint',
Expand Down
30 changes: 30 additions & 0 deletions tests/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,34 @@ describe('utilities/server', () => {

expect(consoleSpy).not.toHaveBeenCalled();
});

test('rewrite extensionless routes to index when single-page rewrites are set', async () => {
const configWithSingle = {
...config,
rewrites: [{ source: '/**/:path([^/.]+)', destination: '/index.html' }],
};

const address = await startServer({ port: 3005 }, configWithSingle, {});
const response = await fetch(`${address.local!}/users/settings`);

expect(response.statusCode).toBe(200);
expect(response.body).toContain('Hello there!');
});

test('do not rewrite dotted request paths when single-page rewrites are set', async () => {
const configWithSingle = {
...config,
rewrites: [{ source: '/**/:path([^/.]+)', destination: '/index.html' }],
};

const address = await startServer({ port: 3006 }, configWithSingle, {});
const extensionResponse = await fetch(`${address.local!}/bundle.js`);
const missingExtensionResponse = await fetch(
`${address.local!}/does-not-exist.js`,
);

expect(extensionResponse.statusCode).toBe(200);
expect(extensionResponse.body.trimEnd()).toBe("console.log('bundle');");
expect(missingExtensionResponse.statusCode).toBe(404);
});
});