Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit a2b9364

Browse files
Support for i18n in Next 10 (#75)
* rough pass at i18n ssg breaking changes in next 10 * update withoutProps/redirects to accommodate defaultLocale * very dirty Just Working i18n for most cases * pre clean-up tests, no cypress yet * make i18n getDataRoute helper more DRY * fix windows test * add documentation for withoutProps and SSR pages logic for i18n * clean up getStaticProps setup logic * fix getStaticProps redirects logic to be consistent with setup and heavily comment * fix revalidate redirects logic and heavily comment * remove superfluous getDataRouteForI18nRoute helper * remove superfluous getFilePathForRouteWithI18n helper * fix existing cypress tests except a few v odd cases * fix/move fallback i18n logic * fix previously commented out failing dataRoute tests * fix 404 cypress tests which expect root level 404.html * specifically test i18n/non-default locales in cypress * root level index pages need special dataRoute logic * use splats for dynamic ssg i18n dataRoutes * plain static routes need 2 redirects: one to defaultLocale, one to preview mode func * massive cleanup/refactor to transform manifest * missing initialProps + fallback logic + PR feedback
1 parent 55178a0 commit a2b9364

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3428
-3488
lines changed

‎cypress/fixtures/next.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
target: "serverless",
2+
target: "experimental-serverless-trace",
33
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
target: "experimental-serverless-trace",
3+
i18n: {
4+
locales: ["en", "fr"],
5+
defaultLocale: "en"
6+
}
7+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default async function preview(req, res) {
2+
const { query } = req;
3+
const { id } = query;
4+
5+
// Enable Preview Mode by setting the cookies
6+
res.setPreviewData({});
7+
8+
// Redirect to the path from the fetched post
9+
// We don't redirect to req.query.slug as that might lead to open redirect vulnerabilities
10+
res.writeHead(307, { Location: `/previewTest/${id}` });
11+
res.end();
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default async function preview(req, res) {
2+
const { query } = req;
3+
const { id } = query;
4+
5+
// Enable Preview Mode by setting the cookies
6+
res.setPreviewData({});
7+
8+
// Redirect to the path from the fetched post
9+
// We don't redirect to req.query.slug as that might lead to open redirect vulnerabilities
10+
res.writeHead(307, { Location: `/previewTest/static` });
11+
res.end();
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default async function exit(_, res) {
2+
// Exit the current user from "Preview Mode". This function accepts no args.
3+
res.clearPreviewData();
4+
5+
// Redirect the user back to the index page.
6+
res.writeHead(307, { Location: "/" });
7+
res.end();
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default async function preview(req, res) {
2+
const { query } = req;
3+
const { to } = query;
4+
5+
res.redirect(`/shows/${to}`);
6+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default async (req, res) => {
2+
// Get the params and query string parameters
3+
const { query } = req;
4+
const { params, ...queryStringParams } = query;
5+
6+
// Get the ID of the show
7+
const id = params[0];
8+
9+
// Get the data
10+
const fetchRes = await fetch(`https://api.tvmaze.com/shows/${id}`);
11+
const data = await fetchRes.json();
12+
13+
// If show was found, return it
14+
if (fetchRes.status == 200) {
15+
res.status(200);
16+
res.json({ params, queryStringParams, show: data });
17+
}
18+
// If show was not found, return error
19+
else {
20+
res.status(404);
21+
res.json({ error: "Show not found" });
22+
}
23+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default async (req, res) => {
2+
// Get the ID of the show
3+
const { query } = req;
4+
const { id } = query;
5+
6+
// Get the data
7+
const fetchRes = await fetch(`https://api.tvmaze.com/shows/${id}`);
8+
const data = await fetchRes.json();
9+
10+
// If show was found, return it
11+
if (fetchRes.status == 200) {
12+
res.status(200);
13+
res.json({ show: data });
14+
}
15+
// If show was not found, return error
16+
else {
17+
res.status(404);
18+
res.json({ error: "Show not found" });
19+
}
20+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default (req, res) => {
2+
// We can set custom headers
3+
res.setHeader("My-Custom-Header", "header123");
4+
5+
res.status(200);
6+
res.json({ message: "hello world :)" });
7+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Error from "next/error";
2+
import Link from "next/link";
3+
4+
const Show = ({ errorCode, show }) => {
5+
// If show item was not found, render 404 page
6+
if (errorCode) {
7+
return <Error statusCode={errorCode} />;
8+
}
9+
10+
// Otherwise, render show
11+
return (
12+
<div>
13+
<p>
14+
This page uses getInitialProps() to fetch the show with the ID provided
15+
in the URL: /shows/:id
16+
<br />
17+
Refresh the page to see server-side rendering in action.
18+
<br />
19+
You can also try changing the ID to any other number between 1-10000.
20+
</p>
21+
22+
<hr />
23+
24+
<h1>Show #{show.id}</h1>
25+
<p>{show.name}</p>
26+
27+
<hr />
28+
29+
<Link href="/">
30+
<a>Go back home</a>
31+
</Link>
32+
</div>
33+
);
34+
};
35+
36+
export const getServerSideProps = async ({ params }) => {
37+
// The ID to render
38+
const { id } = params;
39+
40+
const res = await fetch(`https://api.tvmaze.com/shows/${id}`);
41+
const data = await res.json();
42+
43+
// Set error code if show item could not be found
44+
const errorCode = res.status > 200 ? res.status : false;
45+
46+
return {
47+
props: {
48+
errorCode,
49+
show: data,
50+
},
51+
};
52+
};
53+
54+
export default Show;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /