Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public files not accessible on i18n domains #54765

Closed
1 task done
RobVermeer opened this issue Aug 30, 2023 · 32 comments · Fixed by #60749
Closed
1 task done

Public files not accessible on i18n domains #54765

RobVermeer opened this issue Aug 30, 2023 · 32 comments · Fixed by #60749
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@RobVermeer
Copy link
Contributor

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
    Binaries:
      Node: 20.2.0
      npm: 9.6.4
      Yarn: 1.22.19
      pnpm: 8.7.0
    Relevant Packages:
      next: 13.4.20-canary.12
      eslint-config-next: 13.4.19
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.2.2
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

Internationalization (i18n), Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue or a replay of the bug

https://app.replay.io/recording/reproduction--e17b7c87-6019-40b2-b719-1e9b9ce80b13

To Reproduce

  • Create new next app
  • Add robots.txt in public folder
  • Setup basic i18n (see example below)
  • Make sure you are on different domains (as example)
  • Go to default domain /robots.txt
  • See the robots.txt output
  • Go to different domain /robots.txt
  • See 404
  i18n: {
    defaultLocale: "en",
    locales: ["en", "nl-NL"],
    domains: [
      {
        domain: "com.rob.ticketswap.pizza",
        defaultLocale: "en",
        http: false,
      },
      {
        domain: "nl.rob.ticketswap.pizza",
        defaultLocale: "nl-NL",
        http: false,
      },
    ],
    localeDetection: false,
  },

Describe the Bug

This bug presented itself specifically in Next v13.

Public files are no longer accessible on all domains that are not the default domain (default locale domain).

Expected Behavior

All files in public folder should be accessible on every domain that is setup in the i18n config.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@RobVermeer RobVermeer added the bug Issue was opened via the bug report template. label Aug 30, 2023
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Aug 30, 2023
@cosieLq
Copy link

cosieLq commented Sep 4, 2023

I haven't checked the reproduction but it seems to me similar to #54159

@michalpulpan
Copy link

@RobVermeer

I'm encountering a the same issue 😞. I stumbled upon this GitHub issue after creating local "minimal" example. Since I'm using next-translate-routes in my project I thought it's caused by mentioned library. Instead, it seems to be a bug within Next.js itself.

Have you tried alternative approaches to handle locale domain routing, perhaps using middleware or other methods?

@michalpulpan
Copy link

I don't want to jump to conclusions, but it seems to me that next@13.4.12 is the last version where this issue isn't present.

@RobVermeer
Copy link
Contributor Author

I don't want to jump to conclusions, but it seems to me that next@13.4.12 is the last version where this issue isn't present.

In another project we also have an issue in the latest version, going back to next@13.4.12 fixes that (thanks for the tip @michalpulpan!).

In that project client side routing didn't work anymore, since getServesideProps wasn't called anymore.

I haven't looked into what could have cause all these issues, but it seems something with routing is going on that breaks multiple things.

@jonathandsouza
Copy link

I am facing similar problems with images in the public dir. Any solutions other than dropping the version to 13.4.12?

@dowrow
Copy link

dowrow commented Sep 12, 2023

I found the same bug on version 13.2.1
Fixed it by upgrading next package version to 13.4.12

@arielvieira
Copy link

Having the same issue with the latest versions of next (13.5.4, 13.5.5 and 13.5.6)

I managed to fix this for now by patching the next package with the solution of this PR

diff --git a/node_modules/next/dist/server/lib/router-utils/filesystem.js b/node_modules/next/dist/server/lib/router-utils/filesystem.js
index 635fcdc..f56164e 100644
--- a/node_modules/next/dist/server/lib/router-utils/filesystem.js
+++ b/node_modules/next/dist/server/lib/router-utils/filesystem.js
@@ -348,7 +348,7 @@ async function setupFsCheck(opts) {
                 let locale;
                 let curItemPath = itemPath;
                 let curDecodedItemPath = decodedItemPath;
-                const isDynamicOutput = type === "pageFile" || type === "appFile";
+                const isDynamicOutput = type === 'pageFile' || type === 'appFile' || type === 'publicFolder'
                 if (i18n) {
                     const localeResult = handleLocale(itemPath, // legacy behavior allows visiting static assets under
                     // default locale but no other locale

@erwanriou
Copy link

erwanriou commented Oct 25, 2023

the current fix that i managed to do is that instead of having the robots.txt at the public folder. i generate it within the pages folder as robots.txt.js with the following content:

const RobotsTxt = () => {}

export const getServerSideProps = async ({ res }) => {
  // GENERATE ROBOT DATA
  const robotsTxtData = `User-agent: *\nAllow: /`

  res.setHeader("Content-Type", "text/plain")
  res.write(robotsTxtData)
  res.end()

  return { props: {} }
}

export default RobotsTxt

this way it work with any version of nextjs, localisation and also whenever you have a first level programatical route such as /pages/[slugs] that could conflict with the robots.txt

Now the fact that the whole public folder doesn't work well with i18n remain despite the issue been there for quite some time already...
Basically the public folder cannot be used in // with i18n and next-translate for example. It would only work for the default domain...

My temporary solution was to create a subdomain with nginx to host all the assets and point the links from there.

@goguda
Copy link
Contributor

goguda commented Oct 26, 2023

Having this problem as well! For example, https://www.iroomit.com/fonts/Crimson_Pro/CrimsonPro-Bold.woff2 is accessible, but https://www.iroomit.co.uk/fonts/Crimson_Pro/CrimsonPro-Bold.woff2 is not...

@enmielado
Copy link

Having the same issue. Public folder files are only accessible from the top-level defaultLocale (i18n.defaultLocale).

@ovionlogis
Copy link

I have the same issue in NextJS up to latest version 14.0.1, in dev mode this is not so important, but for production builds via docker I wrote this fix in the Dockerfile. It involves copying the public directory into a locale subdirectory
Original Dockerfile https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile

...
COPY --from=builder /app/public ./public
COPY --from=builder /app/public ./public/{locale} # <-- added this line
...

@goguda
Copy link
Contributor

goguda commented Nov 3, 2023

I have the same issue in NextJS up to latest version 14.0.1, in dev mode this is not so important, but for production builds via docker I wrote this fix in the Dockerfile. It involves copying the public directory into a locale subdirectory Original Dockerfile https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile

...
COPY --from=builder /app/public ./public
COPY --from=builder /app/public ./public/{locale} # <-- added this line
...

We're using docker to deploy too and this works great as a workaround in the meantime. Thank you!

@mbohgard
Copy link

Also having the same issue. We haven't been able to update from 13.3. This is a silly issue with a really small fix. I imagine a lot of people would have this problem?? Please, just fix it already!

@goguda
Copy link
Contributor

goguda commented Nov 23, 2023

Also having the same issue. We haven't been able to update from 13.3. This is a silly issue with a really small fix. I imagine a lot of people would have this problem?? Please, just fix it already!

If it's a really small fix, why not fix it yourself and submit a pr? ;)

@erwanriou
Copy link

erwanriou commented Nov 23, 2023

I think there is a PR already to fix the solution but it was not approved and time passed.

The docker solution works fine in a case that you handle translation within sub urls but if you handle multiple domains then it doesn't work as expected.

The lesson learned here is to not rely on nextjs for assets in multi domain applications and handle it in a S3/gcloud/azure bucket that point to a different sub domain etc.

@aledovskikh
Copy link

aledovskikh commented Nov 24, 2023

This Docker solution (dirty workaround) works well with small /public dir and several locales. But (for example) our /public dir is 200mb and we have 26 locales :(
And yes, there is PR already #55137

@mbohgard
Copy link

@goguda

If it's a really small fix, why not fix it yourself and submit a pr? ;)

  1. There already is a PR, no need to create another one
  2. The issue is known, the solution is known, the fix is tiny - the whole thing is silly
  3. Like @aledovskikh said, the "fix" is not realistic with a big public folder and a lot of locales
  4. Next.js i backed by Vercel - people are getting payed to work on it and people are paying Vercel to host their apps. It shouldn't have to be fixed by the public. Next.js is real product backed by a big company.
  5. It's even more silly what has to be done to fix the problem (duplicating public files)

@kamesdev
Copy link

kamesdev commented Nov 24, 2023

@arielvieira
I used your solution on next 13.5.6v. restarted the server and this still doesn't work.
Upgrading to latest version also doesn't work.

@TheWalkingDad3
Copy link

@kamesdev - we also use the fix with v13.5.6 and it works really great.

We added this line in our Dockerfile to "patch" nextjs:
RUN sed -i 's/const isDynamicOutput.*/const isDynamicOutput = type === "pageFile" || type === "appFile" || type === "publicFolder";/g' /frontend/node_modules/next/dist/server/lib/router-utils/filesystem.js

@arielvieira - thank you very much for this workaround!!

@kamesdev
Copy link

kamesdev commented Nov 24, 2023

What if I don't use docker? How can I do this without docker?

@erwanrioualbelli
Copy link

erwanrioualbelli commented Nov 24, 2023

@kamesdev - we also use the fix with v13.5.6 and it works really great.

We added this line in our Dockerfile to "patch" nextjs: RUN sed -i 's/const isDynamicOutput.*/const isDynamicOutput = type === "pageFile" || type === "appFile" || type === "publicFolder";/g' /frontend/node_modules/next/dist/server/lib/router-utils/filesystem.js

@arielvieira - thank you very much for this workaround!!

Nice, i will tryout on our next release, this might work.

@kamesdev If you don't use docker i assume you still have a release process for a versel/heroku something and that reference to a script within your application right? Generaly its associated to your package.json scripts

just make sure to add a pre-install/pre-build step where you do sed -i 's/const isDynamicOutput.*/const isDynamicOutput = type === "pageFile" || type === "appFile" || type === "publicFolder";/g' /frontend/node_modules/next/dist/server/lib/router-utils/filesystem.js

https://vercel.com/docs/deployments/configure-a-build

@kamesdev
Copy link

kamesdev commented Nov 24, 2023

@kamesdev - we also use the fix with v13.5.6 and it works really great.
We added this line in our Dockerfile to "patch" nextjs: RUN sed -i 's/const isDynamicOutput.*/const isDynamicOutput = type === "pageFile" || type === "appFile" || type === "publicFolder";/g' /frontend/node_modules/next/dist/server/lib/router-utils/filesystem.js
@arielvieira - thank you very much for this workaround!!

Nice, i will tryout on our next release, this might work.

@kamesdev If you don't use docker i assume you still have a release process for a versel/heroku something and that reference to a script within your application right? Generaly its associated to your package.json scripts

just make sure to add a pre-install/pre-build step where you do sed -i 's/const isDynamicOutput.*/const isDynamicOutput = type === "pageFile" || type === "appFile" || type === "publicFolder";/g' /frontend/node_modules/next/dist/server/lib/router-utils/filesystem.js

https://vercel.com/docs/deployments/configure-a-build

@erwanrioualbelli Thanks for reply and solution. Will this work locally with "npm run dev"?

@pekarja5
Copy link
Contributor

@kamesdev You can also use patch-package

@aledovskikh
Copy link

aledovskikh commented Dec 12, 2023

Any progress of releasing fix for this issue?
Also same PR for fix here #59773

@kissifrot
Copy link

I'm bumping this, as we are stuck to NextJS 13.4.12 😅 without this change

@swaffelen
Copy link

It has become crucial for us too, as we are also stuck at version 13.4 🆘

@persocon
Copy link

persocon commented Jan 9, 2024

IF THE FIX WAS TINY WHAT'S TAKING SO LONG?! 5 MONTHS NOW?! WTF GUYS!!! FIX THIS SHIT ALREADY!!!

@persocon
Copy link

persocon commented Jan 9, 2024

didvercelfixedpublicfilesnotaccessibleoni18ndomains.com website coming up soon

@chrisneven
Copy link
Contributor

chrisneven commented Jan 9, 2024

@persocon

Such aggressive and disrespectful language is not constructive and doesn't help at all. Get it together dude.

@chrisneven
Copy link
Contributor

@timneutkens Hallo Tim, could the team take a look at the following #59773? This should solve the issue and even contains tests as requested in #55137.

@JohnConstantin
Copy link

hello, today I encountered the same problem, I'm solving it by copying the public folder, please any update?

COPY --from=build /opt/app/public ./public/en
COPY --from=build /opt/app/public ./public/de
COPY --from=build /opt/app/public ./public/sk

ijjk added a commit that referenced this issue Jan 17, 2024
This ensures we properly handle serving public files when routing via a
locale domain and adds regression tests for this case.

Closes: #54765
Closes: #59773
Closes: #55137
Closes NEXT-2123
Copy link
Contributor

github-actions bot commented Feb 1, 2024

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Feb 1, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet