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.

`unreachable code after return statement` warning

See original GitHub issue

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

14.17.0

What browser are you using?

Firefox

What operating system are you using?

Fedora 34

How are you deploying your application?

next start

Describe the Bug

The browser prints the unreachable code after return statement warning. This only happens in production and not with next dev. The error refers to the last return statement of this piece of code so it has something to do with the i18n routing.

image

Expected Behavior

It doesn’t print the error

To Reproduce

Create a new application with yarn create next-app. Set the following configuration in next.config.js:

module.exports = {
  reactStrictMode: true,
  i18n: {
    defaultLocale: "en",
    locales: ["en"],
  },
}

Run yarn build && yarn start.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
timneutkenscommented, Aug 5, 2021

@smitssjors if you move the last return false into an else it’ll be correctly tree shaken 👍

E.g. this:

export function getDomainLocale(
  path: string,
  locale?: string | false,
  locales?: string[],
  domainLocales?: DomainLocale[]
) {
  if (process.env.__NEXT_I18N_SUPPORT) {
    locale = locale || normalizeLocalePath(path, locales).detectedLocale

    const detectedDomain = detectDomainLocale(domainLocales, undefined, locale)

    if (detectedDomain) {
      return `http${detectedDomain.http ? '' : 's'}://${detectedDomain.domain}${
        basePath || ''
      }${locale === detectedDomain.defaultLocale ? '' : `/${locale}`}${path}`
    }
    return false
  } else {
  	return false
  }
}

Feel free to open a PR changing this. I’ve already verified this will shake it away on https://try.terser.org/

Reference code that is passed to Terser (sort of, it's not exactly this)
// write or paste code here

export function getDomainLocale(
  path,
  locale,
  locales,
  domainLocales
) {
  if (true) {
    locale = locale || normalizeLocalePath(path, locales).detectedLocale

    const detectedDomain = detectDomainLocale(domainLocales, undefined, locale)

    if (detectedDomain) {
      return `http${detectedDomain.http ? '' : 's'}://${detectedDomain.domain}${
        basePath || ''
      }${locale === detectedDomain.defaultLocale ? '' : `/${locale}`}${path}`
    }
    return false
  } else {
  	return false
  }
}
0reactions
balazsorban44commented, Jan 28, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript Warning: unreachable code after return statement
When an expression exists after a valid return statement, a warning is given to indicate that the code after the return statement is...
Read more >
Turn off “unreachable code after return statement” warning
The only way I know of getting around this warning is to put a condition that's always true in the return line: function...
Read more >
Getting 'unreachable code after return statement' error in ...
I'm only receiving the unreachable code warning in production, not on dev, so what would you like in terms of a reproducible demo?...
Read more >
Unreachable Statement Code Error in Java
An unreachable code return statement is typically a sign of a logical error within the program. Although there are several reasons why you...
Read more >
Unreachable Code Error in Java
Have a return statement before them: When a return statement gets executed, then that function execution gets stopped right there. Therefore any ...
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