Skip to content

Commit 7ca959f

Browse files
committed
Show 404 on SSR
1 parent e33bd42 commit 7ca959f

File tree

9 files changed

+33
-21
lines changed

9 files changed

+33
-21
lines changed

config/webpack.config.serverless.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ const imageInlineSizeLimit = parseInt(
2020
process.env.IMAGE_INLINE_SIZE_LIMIT || '10000',
2121
);
2222

23-
console.log(process.env.PUBLIC_URL);
2423
const publicUrl = (paths.servedPath || process.env.PUBLIC_URL).slice(0, -1);
25-
console.log({ publicUrl });
2624
const env = getClientEnvironment(publicUrl);
2725

2826
module.exports = {

src/containers/post/PostViewer.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import gql from 'graphql-tag';
2323
import { shareFacebook, shareTwitter, copyText } from '../../lib/share';
2424
import PostToc from '../../components/post/PostToc';
2525
import LinkedPostList from '../../components/post/LinkedPostList';
26-
import { getScrollTop } from '../../lib/utils';
26+
import { getScrollTop, ssrEnabled } from '../../lib/utils';
2727
import UserProfile from '../../components/common/UserProfile';
2828
import VelogResponsive from '../../components/velog/VelogResponsive';
2929
import styled from 'styled-components';
@@ -106,6 +106,10 @@ const PostViewer: React.FC<PostViewerProps> = ({
106106
});
107107
}, [data, postView, showNotFound]);
108108

109+
if (ssrEnabled && data && data.post === null) {
110+
showNotFound();
111+
}
112+
109113
const prefetchLinkedPosts = useCallback(() => {
110114
if (!data || !data.post) return;
111115
if (prefetched.current) return;

src/containers/tags/TagDetailContainer.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import TagDetail, { TagDetailSkeleton } from '../../components/tags/TagDetail';
33
import { useQuery } from '@apollo/react-hooks';
44
import { GET_TAG, GetTagResponse } from '../../lib/graphql/tags';
55
import { GET_POST_LIST, PartialPost } from '../../lib/graphql/post';
6-
import { safe } from '../../lib/utils';
6+
import { safe, ssrEnabled } from '../../lib/utils';
77
import useScrollPagination from '../../lib/hooks/useScrollPagination';
88
import PostCardList, {
99
PostCardListSkeleton,
@@ -35,6 +35,10 @@ function TagDetailContainer({ tag }: TagDetailContainerProps) {
3535
}
3636
}, [showNotFound, tagDetail.data]);
3737

38+
if (ssrEnabled && tagDetail.data && !tagDetail.data.tag) {
39+
showNotFound();
40+
}
41+
3842
const onLoadMore = useCallback(
3943
(cursor: string) => {
4044
getPostList.fetchMore({

src/containers/velog/SeriesPosts.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import SeriesActionButtons from '../../components/velog/SeriesActionButtons';
1818
import DraggableSeriesPostList from '../../components/velog/DraggableSeriesPostList';
1919
import useUser from '../../lib/hooks/useUser';
2020
import useNotFound from '../../lib/hooks/useNotFound';
21+
import { ssrEnabled } from '../../lib/utils';
2122

2223
export interface SeriesPostsProps {
2324
username: string;
@@ -63,6 +64,10 @@ const SeriesPosts: React.FC<SeriesPostsProps> = ({ username, urlSlug }) => {
6364
setOrder(data.series.series_posts.map(sp => sp.id));
6465
}, [data, showNotFound]);
6566

67+
if (ssrEnabled && data && !data.series) {
68+
showNotFound();
69+
}
70+
6671
const onChangeNextName = useCallback(
6772
(e: React.FormEvent<HTMLHeadingElement>) => {
6873
setNextName(e.currentTarget.innerText);

src/containers/velog/hooks/useApplyVelogConfig.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,8 @@ export default function useApplyVelogConfig(username: string) {
6161
}),
6262
);
6363
}
64+
65+
if (ssrEnabled && data && data.velog_config === null) {
66+
showNotFound();
67+
}
6468
}

src/server/sampleRender.tsx

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/server/serverRender.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ const serverRender = async ({ url, loggedIn, cookie }: SSROption) => {
5757
cache: new InMemoryCache(),
5858
});
5959

60-
const context = {};
60+
const context = {
61+
statusCode: 200,
62+
};
6163
const sheet = new ServerStyleSheet();
6264
const extractor = new ChunkExtractor({
6365
statsFile,
@@ -108,7 +110,7 @@ const serverRender = async ({ url, loggedIn, cookie }: SSROption) => {
108110
}
109111
});
110112

111-
return pageHtml;
113+
return { html: pageHtml, statusCode: context.statusCode };
112114
};
113115

114116
export default serverRender;

src/server/ssrMiddleware.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import serverRender from './serverRender';
33

44
const ssrMiddleware: Middleware = async (ctx, next) => {
55
try {
6-
const html = await serverRender({
6+
const result = await serverRender({
77
url: ctx.url,
88
cookie: ctx.headers.cookie,
99
loggedIn: !!(
1010
ctx.cookies.get('refresh_token') || ctx.cookies.get('access_token')
1111
),
1212
});
13-
if (!html) {
13+
if (!result) {
1414
return next();
1515
}
16-
ctx.body = html;
16+
ctx.body = result.html;
17+
ctx.status = result.statusCode;
1718
} catch (e) {
1819
ctx.throw(500, e);
1920
}

src/serverless.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ export const handler = async (event: APIGatewayEvent) => {
1212
cookie.includes('refresh_token') || cookie.includes('access_token');
1313

1414
try {
15-
const html = await serverRender({
15+
const result = await serverRender({
1616
url,
1717
cookie,
1818
loggedIn,
1919
});
20+
if (!result) throw new Error('Result is null');
21+
22+
const { html, statusCode } = result;
23+
2024
return {
21-
statusCode: 200,
25+
statusCode: statusCode,
2226
headers: {
2327
'content-type': 'text/html; charset=utf-8;',
2428
},

0 commit comments

Comments
 (0)