adds handling for draft and published pages

This commit is contained in:
ewatch 2021-06-21 18:40:40 +02:00 committed by Nate Moore
parent 2aec0b8a6f
commit 5c6c6c39bc
2 changed files with 19 additions and 1 deletions

View file

@ -92,6 +92,7 @@ export async function buildStaticPage({ astroConfig, buildState, filepath, runti
const { pages: pagesRoot } = astroConfig;
const url = filepath.pathname.replace(pagesRoot.pathname, '/').replace(/(index)?\.(astro|md)$/, '');
const result = await runtime.load(url);
if (result.statusCode === 404 && result.error.message === 'Unpublished document') return;
if (result.statusCode !== 200) throw new Error((result as any).error);
const outFile = path.posix.join(url, '/index.html');
buildState[outFile] = {

View file

@ -50,7 +50,7 @@ type LoadResultNotFound = { statusCode: 404; error: Error; collectionInfo?: Coll
type LoadResultRedirect = { statusCode: 301 | 302; location: string; collectionInfo?: CollectionInfo };
type LoadResultError = { statusCode: 500 } & ({ type: 'parse-error'; error: CompileError } | { type: 'not-found'; error: CompileError } | { type: 'unknown'; error: Error });
export type LoadResult = (LoadResultSuccess | LoadResultNotFound | LoadResultRedirect | LoadResultError) & { collectionInfo?: CollectionInfo };
export type LoadResult = (LoadResultSuccess | LoadResultNotFound | LoadResultRedirect | LoadResultError) & { collectionInfo?: CollectionInfo };
// Disable snowpack from writing to stdout/err.
configureSnowpackLogger(snowpackLogger);
@ -105,6 +105,23 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro
let collection = {} as CollectionResult;
let additionalURLs = new Set<string>();
if (mod.exports.__content) {
const hasDraftAttribute = mod.exports.__content.hasOwnProperty('draft');
const hasPublishAttribute = mod.exports.__content.hasOwnProperty('published');
if(hasPublishAttribute && mod.exports.__content['published'] === false) {
return {
statusCode: 404,
error: new Error('Unpublished document')
};
}
if(hasDraftAttribute && mod.exports.__content['draft'] === true && buildOptions.draft && config.mode === 'production') {
return {
statusCode: 404,
error: new Error('Unpublished document')
};
}
}
if (mod.exports.createCollection) {
const createCollection: CreateCollection = await mod.exports.createCollection();
const VALID_KEYS = new Set(['data', 'routes', 'permalink', 'pageSize', 'rss']);