fix: use 403 error for unpublished docs
This commit is contained in:
parent
c65b92c176
commit
ad8e937454
3 changed files with 29 additions and 9 deletions
|
@ -92,7 +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 === 403) return;
|
||||
if (result.statusCode !== 200) throw new Error((result as any).error);
|
||||
const outFile = path.posix.join(url, '/index.html');
|
||||
buildState[outFile] = {
|
||||
|
|
|
@ -50,6 +50,25 @@ export default async function dev(astroConfig: AstroConfig) {
|
|||
res.end();
|
||||
break;
|
||||
}
|
||||
case 403: {
|
||||
const fullurl = new URL(req.url || '/', astroConfig.buildOptions.site || `http://localhost${astroConfig.devOptions.port}`);
|
||||
const reqPath = decodeURI(fullurl.pathname);
|
||||
error(logging, 'static', 'Forbidden', reqPath);
|
||||
res.statusCode = 403;
|
||||
|
||||
const fourOhThreeResult = await runtime.load('/403');
|
||||
if (fourOhThreeResult.statusCode === 200) {
|
||||
if (fourOhThreeResult.contentType) {
|
||||
res.setHeader('Content-Type', fourOhThreeResult.contentType);
|
||||
}
|
||||
res.write(fourOhThreeResult.contents);
|
||||
} else {
|
||||
res.setHeader('Content-Type', 'text/plain');
|
||||
res.write(`Forbidden: ${result.error.message}`);
|
||||
}
|
||||
res.end();
|
||||
break;
|
||||
}
|
||||
case 404: {
|
||||
const fullurl = new URL(req.url || '/', astroConfig.buildOptions.site || `http://localhost${astroConfig.devOptions.port}`);
|
||||
const reqPath = decodeURI(fullurl.pathname);
|
||||
|
|
|
@ -46,11 +46,12 @@ type LoadResultSuccess = {
|
|||
contents: string | Buffer;
|
||||
contentType?: string | false;
|
||||
};
|
||||
type LoadResultForbidden = { statusCode: 403; error: Error; collectionInfo?: CollectionInfo };
|
||||
type LoadResultNotFound = { statusCode: 404; error: Error; collectionInfo?: CollectionInfo };
|
||||
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 | LoadResultForbidden | LoadResultNotFound | LoadResultRedirect | LoadResultError) & { collectionInfo?: CollectionInfo };
|
||||
|
||||
// Disable snowpack from writing to stdout/err.
|
||||
configureSnowpackLogger(snowpackLogger);
|
||||
|
@ -110,14 +111,14 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro
|
|||
const hasPublishAttribute = mod.exports.__content.hasOwnProperty('published');
|
||||
if (hasPublishAttribute && mod.exports.__content['published'] === false) {
|
||||
return {
|
||||
statusCode: 404,
|
||||
error: new Error('Unpublished document')
|
||||
statusCode: 403,
|
||||
error: new Error('Document is not published')
|
||||
};
|
||||
}
|
||||
if (hasDraftAttribute && mod.exports.__content['draft'] === true && !buildOptions.draft && config.mode === 'production') {
|
||||
return {
|
||||
statusCode: 404,
|
||||
error: new Error('Unpublished document')
|
||||
statusCode: 403,
|
||||
error: new Error('Document is not published')
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue