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 { pages: pagesRoot } = astroConfig;
|
||||||
const url = filepath.pathname.replace(pagesRoot.pathname, '/').replace(/(index)?\.(astro|md)$/, '');
|
const url = filepath.pathname.replace(pagesRoot.pathname, '/').replace(/(index)?\.(astro|md)$/, '');
|
||||||
const result = await runtime.load(url);
|
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);
|
if (result.statusCode !== 200) throw new Error((result as any).error);
|
||||||
const outFile = path.posix.join(url, '/index.html');
|
const outFile = path.posix.join(url, '/index.html');
|
||||||
buildState[outFile] = {
|
buildState[outFile] = {
|
||||||
|
|
|
@ -50,6 +50,25 @@ export default async function dev(astroConfig: AstroConfig) {
|
||||||
res.end();
|
res.end();
|
||||||
break;
|
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: {
|
case 404: {
|
||||||
const fullurl = new URL(req.url || '/', astroConfig.buildOptions.site || `http://localhost${astroConfig.devOptions.port}`);
|
const fullurl = new URL(req.url || '/', astroConfig.buildOptions.site || `http://localhost${astroConfig.devOptions.port}`);
|
||||||
const reqPath = decodeURI(fullurl.pathname);
|
const reqPath = decodeURI(fullurl.pathname);
|
||||||
|
|
|
@ -46,11 +46,12 @@ type LoadResultSuccess = {
|
||||||
contents: string | Buffer;
|
contents: string | Buffer;
|
||||||
contentType?: string | false;
|
contentType?: string | false;
|
||||||
};
|
};
|
||||||
|
type LoadResultForbidden = { statusCode: 403; error: Error; collectionInfo?: CollectionInfo };
|
||||||
type LoadResultNotFound = { statusCode: 404; error: Error; collectionInfo?: CollectionInfo };
|
type LoadResultNotFound = { statusCode: 404; error: Error; collectionInfo?: CollectionInfo };
|
||||||
type LoadResultRedirect = { statusCode: 301 | 302; location: string; 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 });
|
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.
|
// Disable snowpack from writing to stdout/err.
|
||||||
configureSnowpackLogger(snowpackLogger);
|
configureSnowpackLogger(snowpackLogger);
|
||||||
|
@ -110,14 +111,14 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro
|
||||||
const hasPublishAttribute = mod.exports.__content.hasOwnProperty('published');
|
const hasPublishAttribute = mod.exports.__content.hasOwnProperty('published');
|
||||||
if (hasPublishAttribute && mod.exports.__content['published'] === false) {
|
if (hasPublishAttribute && mod.exports.__content['published'] === false) {
|
||||||
return {
|
return {
|
||||||
statusCode: 404,
|
statusCode: 403,
|
||||||
error: new Error('Unpublished document')
|
error: new Error('Document is not published')
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (hasDraftAttribute && mod.exports.__content['draft'] === true && !buildOptions.draft && config.mode === 'production') {
|
if (hasDraftAttribute && mod.exports.__content['draft'] === true && !buildOptions.draft && config.mode === 'production') {
|
||||||
return {
|
return {
|
||||||
statusCode: 404,
|
statusCode: 403,
|
||||||
error: new Error('Unpublished document')
|
error: new Error('Document is not published')
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue