question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Namespace not being loaded for error page with default-prefix middleware

See original GitHub issue

What version of Next.js are you using?

12.0.4-canary.5

What version of Node.js are you using?

v16.13.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

next start

Describe the Bug

I see not translated strings at error page using middleware to prefix default locale

Expected Behavior

I expected to see translated strings with loaded namespace

To Reproduce

  1. Use middleware to prefix the default locale
  2. Try to open any not-existing file from public folder (without locale prefix): http://localhost:3031/common/this_file_not_exists.png
  3. There will be no redirect to locale-prefixed path because of the check for a file at middleware config
  4. You will see the output from the _error.tsx page.
  5. Try to output some translatable strings on this error page
  6. You’ll see “common:error.title” instead of the translated strings
  7. All strings are translated OK with locale-prefixed path like: http://localhost:3031/ru/common/test.png

My i18n.json:

{
  "locales": ["default", "ru", "en"],
  "defaultLocale": "default",
  "pages": {
    "*": ["common", "main"],
    "/_error": ["common", "main"]
  }
}

And /pages/_middleware.ts:

import { NextRequest, NextResponse } from 'next/server'

const PUBLIC_FILE = /\.(.*)$/
const DEFAULT_LOCALE = 'en'

export function middleware(request: NextRequest) {
  const pathName = request.nextUrl.pathname
  const shouldHandleLocale =
    !PUBLIC_FILE.test(pathName) && request.nextUrl.locale === 'default'
  return shouldHandleLocale
    ? NextResponse.redirect(`/${DEFAULT_LOCALE}${pathName}`)
    : undefined
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ajmnzcommented, Nov 11, 2021

Just for context, this is the error

next-translate - compiled page: /404 - locale: default - namespaces: common - used loader: getStaticProps
SerializableError: Error serializing `.__namespaces.common` returned from `getStaticProps` in "/404".
Reason: `undefined` cannot be serialized as JSON. Please use `null` or omit this value.
    at isSerializable (/***/node_modules/next/dist/lib/is-serializable-props.js:39:19)
    at /***/node_modules/next/dist/lib/is-serializable-props.js:46:66
    at Array.every (<anonymous>)
    at isSerializable (/***/node_modules/next/dist/lib/is-serializable-props.js:43:39)
    at /***/node_modules/next/dist/lib/is-serializable-props.js:46:66
    at Array.every (<anonymous>)
    at isSerializable (/***/node_modules/next/dist/lib/is-serializable-props.js:43:39)
    at Object.isSerializableProps (/***/node_modules/next/dist/lib/is-serializable-props.js:66:12)
    at Object.renderToHTML (/***/node_modules/next/dist/server/render.js:418:93)
    at async doRender (/***/node_modules/next/dist/server/next-server.js:1389:38)

It would be nice if there was a property like ignoreDefault: boolean, that would just ignore the default locale.

1reaction
yfhuicommented, May 25, 2022

hi guys, using 1.4.0 of next-translate now.

After setup with localesToIgnore, But I still got the default locale with a 404 page complied at the very first time it running npm run dev (should be the compliation triggered by getStaticProps )

Here is the sinppet:

// i18n.js
module.exports = {
    defaultLocale: "default",
    locales: ["default", "en-us"],
    localeDetection: false,
    localesToIgnore: ["default"],
    // ...
};
// 404.js
// ...
export default function Custom404() {
   // ...
}
export const getStaticProps = async ({ req, res, query, locale }) => {
    console.log(locale) // it prints "default"
    return { props: {} };
};

May I know what is wrong in my config? Or there’s other issues we didn’t spot it in this thread?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Namespace not being loaded for error page with default-prefix ...
To Reproduce. Use middleware to prefix the default locale; Try to open any not-existing file from public folder (without locale prefix): http ...
Read more >
load(): Namespace default prefix was not found in Entity ...
A workaround (and possible permanent solution) is that of adding the namespace to each of the <!ENTITY> s, like so:
Read more >
11 Oracle WebLogic Server
Oracle Fusion Middleware 11g contains Oracle WebLogic Server 11g. The version number of ... Section 11.2.1, "Cached JDBC Information is not Displayed".
Read more >
CICS TS for z/OS: Using Web Services with CICS - IBM
Not every request is succeeded by a response; some applications use a ... immediate transition can also occur if CICS detects an error....
Read more >
2007-July.txt - Python mailing list
The default prefix is "app:"; you can also give "middleware:". Prefixes not recognized will be ignored. A possible prefix might be "logging:" for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found