如果请求的页面没有底层服务器端资源,我正在服务器端呈现页面中寻找处理HTTP 404的最佳实践.
例如,假设请求的页面为http://localhost:3000/places/5
.在我的SSG实施中:
export async function getServerSideProps(context) {
const placeId = context.params.placeId;
const places = await getPlace(placeId);
if (!places.length) { /* is there anything here I can do to facilitate a 404? this place does not exist in the db */ }
return {
props: {
places[0],
},
};
}
应该是不言自明的,但如果请求的id(在本例中为5
)不是我数据库中的一个位置,我如何将其作为HTTP 404处理?