diff --git a/packages/astro/src/core/path.ts b/packages/astro/src/core/path.ts index b738fe7fe..2d6f62dff 100644 --- a/packages/astro/src/core/path.ts +++ b/packages/astro/src/core/path.ts @@ -1,6 +1,5 @@ - export function appendForwardSlash(path: string) { - return path.endsWith('/') ? path : path + '/'; + return path.endsWith('/') ? path : path + '/'; } export function prependForwardSlash(path: string) { @@ -29,7 +28,7 @@ export function isRelativePath(path: string) { } export function prependDotSlash(path: string) { - if(isRelativePath(path)) { + if (isRelativePath(path)) { return path; } diff --git a/packages/astro/src/core/preview/index.ts b/packages/astro/src/core/preview/index.ts index 50f9fb619..aa742b716 100644 --- a/packages/astro/src/core/preview/index.ts +++ b/packages/astro/src/core/preview/index.ts @@ -12,7 +12,6 @@ import { prependForwardSlash } from '../path.js'; import * as npath from 'path'; import * as fs from 'fs'; - interface PreviewOptions { logging: LogOptions; } @@ -27,10 +26,10 @@ export interface PreviewServer { type SendStreamWithPath = send.SendStream & { path: string }; function removeBase(base: string, pathname: string) { - if(base === pathname) { - return '/'; - } - let requrl = pathname.substr(base.length); + if (base === pathname) { + return '/'; + } + let requrl = pathname.substr(base.length); return prependForwardSlash(requrl); } @@ -47,27 +46,31 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO return; } - switch(config.devOptions.trailingSlash) { + switch (config.devOptions.trailingSlash) { case 'always': { - if(!req.url?.endsWith('/')) { + if (!req.url?.endsWith('/')) { res.statusCode = 404; - res.end(template({ - title: 'Not found', - tabTitle: 'Not found', - pathname: req.url!, - })); + res.end( + template({ + title: 'Not found', + tabTitle: 'Not found', + pathname: req.url!, + }) + ); return; } break; } case 'never': { - if(req.url?.endsWith('/')) { + if (req.url?.endsWith('/')) { res.statusCode = 404; - res.end(template({ - title: 'Not found', - tabTitle: 'Not found', - pathname: req.url!, - })); + res.end( + template({ + title: 'Not found', + tabTitle: 'Not found', + pathname: req.url!, + }) + ); return; } break; @@ -79,13 +82,13 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO let sendpath = removeBase(base, req.url!); const sendOptions: send.SendOptions = { - root: fileURLToPath(config.dist) + root: fileURLToPath(config.dist), }; - if(config.buildOptions.pageUrlFormat === 'file' && !sendpath.endsWith('.html')) { + if (config.buildOptions.pageUrlFormat === 'file' && !sendpath.endsWith('.html')) { sendOptions.index = false; const parts = sendpath.split('/'); let lastPart = parts.pop(); - switch(config.devOptions.trailingSlash) { + switch (config.devOptions.trailingSlash) { case 'always': { lastPart = parts.pop(); break; @@ -96,7 +99,7 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO } case 'ignore': { // this could end in slash, so resolve either way - if(lastPart === '') { + if (lastPart === '') { lastPart = parts.pop(); } break; @@ -106,14 +109,14 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO sendpath = npath.sep + npath.join(...parts, `${part}.html`); } send(req, sendpath, sendOptions) - .once('directory', function(this: SendStreamWithPath, _res, path) { - if(config.buildOptions.pageUrlFormat === 'directory' && !path.endsWith('index.html')) { - return this.sendIndex(path); - } else { - this.error(404); - } - }) - .pipe(res); + .once('directory', function (this: SendStreamWithPath, _res, path) { + if (config.buildOptions.pageUrlFormat === 'directory' && !path.endsWith('index.html')) { + return this.sendIndex(path); + } else { + this.error(404); + } + }) + .pipe(res); }); let { hostname, port } = config.devOptions; diff --git a/packages/astro/src/vite-plugin-build-html/index.ts b/packages/astro/src/vite-plugin-build-html/index.ts index 42aba3c93..acb2c87cf 100644 --- a/packages/astro/src/vite-plugin-build-html/index.ts +++ b/packages/astro/src/vite-plugin-build-html/index.ts @@ -80,8 +80,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin { } for (const pathname of pageData.paths) { - const pathrepl = astroConfig.buildOptions.pageUrlFormat === 'directory' ? - '/index.html' : pathname === '/' ? 'index.html' : '.html'; + const pathrepl = astroConfig.buildOptions.pageUrlFormat === 'directory' ? '/index.html' : pathname === '/' ? 'index.html' : '.html'; pageNames.push(pathname.replace(/\/?$/, pathrepl).replace(/^\//, '')); const id = ASTRO_PAGE_PREFIX + pathname; const html = await ssrRender(renderers, mod, { diff --git a/packages/astro/test/preview-routing.test.js b/packages/astro/test/preview-routing.test.js index 3e86ec4ad..8c94a6879 100644 --- a/packages/astro/test/preview-routing.test.js +++ b/packages/astro/test/preview-routing.test.js @@ -14,8 +14,8 @@ describe('Preview Routing', () => { projectRoot: './fixtures/with-subpath-no-trailing-slash/', devOptions: { trailingSlash: 'never', - port: 4000 - } + port: 4000, + }, }); await fixture.build(); previewServer = await fixture.preview(); @@ -68,8 +68,8 @@ describe('Preview Routing', () => { projectRoot: './fixtures/with-subpath-no-trailing-slash/', devOptions: { trailingSlash: 'always', - port: 4001 - } + port: 4001, + }, }); await fixture.build(); previewServer = await fixture.preview(); @@ -127,8 +127,8 @@ describe('Preview Routing', () => { projectRoot: './fixtures/with-subpath-no-trailing-slash/', devOptions: { trailingSlash: 'ignore', - port: 4002 - } + port: 4002, + }, }); await fixture.build(); previewServer = await fixture.preview(); @@ -187,12 +187,12 @@ describe('Preview Routing', () => { fixture = await loadFixture({ projectRoot: './fixtures/with-subpath-no-trailing-slash/', buildOptions: { - pageUrlFormat: 'file' + pageUrlFormat: 'file', }, devOptions: { trailingSlash: 'never', - port: 4003 - } + port: 4003, + }, }); await fixture.build(); previewServer = await fixture.preview(); @@ -244,12 +244,12 @@ describe('Preview Routing', () => { fixture = await loadFixture({ projectRoot: './fixtures/with-subpath-no-trailing-slash/', buildOptions: { - pageUrlFormat: 'file' + pageUrlFormat: 'file', }, devOptions: { trailingSlash: 'always', - port: 4004 - } + port: 4004, + }, }); await fixture.build(); previewServer = await fixture.preview(); @@ -306,12 +306,12 @@ describe('Preview Routing', () => { fixture = await loadFixture({ projectRoot: './fixtures/with-subpath-no-trailing-slash/', buildOptions: { - pageUrlFormat: 'file' + pageUrlFormat: 'file', }, devOptions: { trailingSlash: 'ignore', - port: 4005 - } + port: 4005, + }, }); await fixture.build(); previewServer = await fixture.preview(); @@ -368,12 +368,12 @@ describe('Preview Routing', () => { fixture = await loadFixture({ projectRoot: './fixtures/with-subpath-no-trailing-slash/', buildOptions: { - pageUrlFormat: 'file' + pageUrlFormat: 'file', }, devOptions: { trailingSlash: 'ignore', - port: 4006 - } + port: 4006, + }, }); await fixture.build(); previewServer = await fixture.preview(); @@ -398,7 +398,6 @@ describe('Preview Routing', () => { expect(response.status).to.equal(200); }); - it('200 when loading dynamic route', async () => { const response = await fixture.fetch('/blog/1.html'); expect(response.status).to.equal(200);