Comments (5)
Ok, I see. Is the getServerSideProps
function declared for the 404 page then? I'm unsure since you mention that you need the API route during build time, but the presence of that function should cause the page to be run only at runtime (or is there an exception for 404 pages in Next.js to always build statically)? I haven't used Next.js API routes much so far, but I could definitely imagine that they don't mount at build time (like you mention it).
There's quite a bit of guesswork here. Generally this seems to be an issue caused by the usage of Next.js framework features and less by this library. If you can provide a reproduction I might be able to help you, but other than that you could also consider accessing the JSON files via a module import statement – as you've also mentioned.
from next-intl.
Hi @andrevenancio and thanks for the report!
The 404 page works the same way as the other pages, I've provided an example in #66. I've also improved the error message a bit when no messages are configured at all – I think that's the issue you were seeing initially.
Do you want to have a look and let me know if this works for you?
from next-intl.
Hi @amannn Thanks for your reply. I think this solution does work, but I'll explain how I had implemented it, and hopefully It helps shine a light to:
- Either a bug
- Me doing something wrong.
So on my projects I have 2 json (en
and de
). They live on src/locales/
.
But instead of accessing the files directly like you do on your example:
export async function getStaticProps({locale}: GetStaticPropsContext) {
return {
props: {
messages: pick(
await import(`../../messages/${locale}.json`),
NotFound.messages
)
}
};
}
I'm doing something different... I'm calling the api
which loads the json files for me instead. So my request ends up looking like:
export const getServerSideProps: GetServerSideProps = async ({ locale }) => {
return {
props: {
messages: await fetch(`${process.env.DOMAIN}/api/language/${locale}`).then((e) => e.json()),
},
};
};
And just for reference my api call is just a simple
import { NextApiRequest, NextApiResponse } from 'next';
import messages from '@locales/en.json';
export default async (_req: NextApiRequest, res: NextApiResponse) => {
res.setHeader('Cache-Control', 's-maxage=300, public');
res.status(200).json(messages);
};
Now, this works on development and on production except on the 404 page. I can't be sure to why, but it seems like localhost:3000 isn't running on build time for the 404 page hence my initial errors I shared on twitter that seem to come from node-fetch.
Using your solution works fine. I wonder if I should just scrap the API idea and access the json files directly.
from next-intl.
I think 404 pages are an exception on nextjs
. I'll close this issue. I think you're right this is a nextjs
issue not a next-intl
issue.
Thank you!
from next-intl.
I think 404 pages are an exception
I think so too, yes, based on your pasted build output:
├ ○ /404 429 B 675 kB
○ (Static) automatically rendered as static HTML (uses no initial props)
from next-intl.
Related Issues (20)
- Switching language in sub-routes with dynamic routes throws an error HOT 6
- Importing createMiddleware() doesn't work as the tutorial suggests in type:module packages HOT 3
- Convenience injection of rewrites & exportPathMap HOT 3
- Domain based routing problem. HOT 2
- Verbose `localePrefix` does not redirect to the custom prefix HOT 3
- Allow for handling optional/invalid numbers on plural interpolation HOT 1
- Googlebot doesn't receieve translations HOT 2
- Middleware not running in NX monorepo project, causing "Unable to find `next-intl` locale" error HOT 8
- There is a problem with translation when the language family name has Hyphen HOT 1
- [Docs]: It is not clear to add pathnames to middleware file if you want to use localized pathnames HOT 1
- `redirect` from `createSharedPathnamesNavigation` doesn't handle relative paths HOT 1
- No default component was found HOT 1
- Allow usage of markup in addition to rich values in defaultTranslationValues HOT 3
- No any logic for `trailingSlash: true` HOT 5
- Inability to override locale on Date formatting results in incorrect localization HOT 2
- Using a multiples json in aplication HOT 1
- Router.replace from createLocalizedPathnamesNavigation causes infinite render HOT 1
- Open redirect vulnerability exposed when `localePrefix: 'as-needed'` HOT 1
- [Docs]: Confused about 2 domains HOT 2
- Build Error from the main core file. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-intl.