unkeyed / unkey

Open source API management platform
https://go.unkey.com
Other
4k stars 468 forks source link

Non existent blog pages throw a 500 #940

Closed chronark closed 8 months ago

chronark commented 8 months ago

Preliminary Checks

Reproduction / Replay Link (Optional)

No response

Issue Summary

/blog/\\\\\\\[slug\\\\\\\] pages throw a 500 error when the blog does not exist

Steps to Reproduce

Go to https://unkey.dev/blog/xxx

Expected behavior

It should redirect to /blog

Other information

Solution idea: Catch the fs.readFile error and redirect

Screenshots

No response

Version info

- OS:
- Node:
- npm:
linear[bot] commented 8 months ago

ENG-503 Non existent blog pages throw a 500

AkshayBandi027 commented 8 months ago

hey @chronark, I am able to solve the issue by making some changes in raw function in the mdx-helper.tsx. I have add to try and catch block so whenever there no filepath for slug then it redirects to blog

 `export const raw = async ({contentPath,filepath,}: {contentPath: string;filepath: string;}) => {`try {
 const fileContent = await fs.readFile(`${contentPath}/${filepath}.mdx`, "utf-8");
 return fileContent } catch(error){
  redirect(`/blog`)}};`

https://github.com/unkeyed/unkey/assets/100031493/f3ca524b-8e0a-4f93-ba3f-4e9f7d2eb12c

chronark commented 8 months ago

lgtm cc @perkinsjr

perkinsjr commented 8 months ago

It should probably 404 and not redirect back to the blog. The rest is the correct approach.

AkshayBandi027 commented 8 months ago

@perkinsjr ok I can make changes and submit a Pull request ?

perkinsjr commented 8 months ago

Yup

AkshayBandi027 commented 8 months ago

hey @perkinsjr,

https://github.com/unkeyed/unkey/assets/100031493/1dfe809e-d1e4-4a47-b476-8e8fb81de411