Skip to content

Commit

Permalink
Error on preview if outDir missing (#9544)
Browse files Browse the repository at this point in the history
* Error on preview if outDir missing

* Update .changeset/four-moles-burn.md

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

---------

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
  • Loading branch information
3 people authored Jan 3, 2024
1 parent ab6049b commit b8a6fa8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-moles-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Adds a helpful error for static sites when you use the `astro preview` command if you have not previously run `astro build`.
7 changes: 7 additions & 0 deletions packages/astro/src/core/preview/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'node:fs';
import { createRequire } from 'node:module';
import { fileURLToPath, pathToFileURL } from 'node:url';
import type { AstroInlineConfig, PreviewModule, PreviewServer } from '../../@types/astro.js';
Expand Down Expand Up @@ -34,6 +35,12 @@ export default async function preview(inlineConfig: AstroInlineConfig): Promise<
await runHookConfigDone({ settings: settings, logger: logger });

if (settings.config.output === 'static') {
if (!fs.existsSync(settings.config.outDir)) {
const outDirPath = fileURLToPath(settings.config.outDir);
throw new Error(
`[preview] The output directory ${outDirPath} does not exist. Did you run \`astro build\`?`
);
}
const server = await createStaticPreviewServer(settings, logger);
return server;
}
Expand Down

0 comments on commit b8a6fa8

Please sign in to comment.