Next-translate: Bug when wrapping index in folder

Created on 9 Jun 2020  ยท  30Comments  ยท  Source: vinissimus/next-translate

Hello, we found a bug just before the deploy that blocks us from using this lib :(.

If we wrap the pages_/index.tsx inside a folder, like index/index.tsx it will break on all the languages but default one when building static. Navigating to site.com/es throws a weird error on compiled js files.

How to reproduce

  1. Configure two or more languages
  2. Wrap your index page inside a folder pages_/index/index.tsx
  3. Build static and export
  4. Run a local server inside the dist folder and manually navigate to the index of any of the non-default language, like localhost:3000/es/

Output

dist/
โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ my-page.html
โ”œโ”€โ”€ es.html
โ”œโ”€โ”€ es/
โ””โ”€โ”€ โ””โ”€โ”€ my-page.html

Expected output

dist/
โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ my-page.html
โ”œโ”€โ”€ es/
โ”œโ”€โ”€ โ””โ”€โ”€ index.html
โ””โ”€โ”€ โ””โ”€โ”€ my-page.html
bug

All 30 comments

It would be great to provide more info: next-translate version, config, your pages tree ๐Ÿ‘

I tried to reproduce it but I get a different result in my case:

Generated pages from _pages:

pages
โ”œโ”€โ”€ es
โ”‚ย ย  โ”œโ”€โ”€ index
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ index.js
โ”œโ”€โ”€ index
โ”‚ย ย  โ””โ”€โ”€ index.js

out after yarn build && yarn export

out
โ”œโ”€โ”€ es
โ”‚ย ย  โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ favicon.ico
โ”œโ”€โ”€ index.html

Yeah, sorry.

v0.17.1

Script:
"build": "next-translate && next build && next export -o dist/",

i18n.json

{
  "allLanguages": ["en", "it"],
  "defaultLanguage": "en",
  "currentPagesDir": "pages_",
  "finalPagesDir": "pages",
  "localesPath": "locales",
  "pages": {
    "*": ["common"],
    "/": ["home"],
    "/covid-tech": ["covid-tech"]
  }
}

pages_ structure is:

Screenshot 2020-06-09 at 18 31 55

The issue is not the output. With this structure the generated files are ok, but as mentioned above if we navigate to a language, we get this weird error:

Screenshot 2020-06-09 at 18 33 21


This forces us to revert the structure to a flat index, that works as expected, but the flat index doesn't generate an index.html inside the language folder.

Additional bug

With the index as a folder, the above configuration doesn't work anymore (keys are not replaced) for the default language path. We have to set / to /index, like so:

  "pages": {
    "*": ["common"],
    "/index": ["home"],
    "/covid-tech": ["covid-tech"]
  }

I think the issue is related to the module.css. Looks that we are not ignoring css and scss files on the build step:

image

To confirm this, try to move the index.module.css outside pages_ folder. I guess this should work.

Nope :( it doesn't seem to solve the issue. I tried:

  1. To move the css inside the src folder
  2. Same as the first point, but removing also the module from the filename
  3. Deleting the file entirely

Nothing is changing ๐Ÿ˜ž. It doesn't seem related to the css.

I've added an additional bug in this comment, but don't know if it's related to this one.

Ok, looks that the css and scss that I detected was an extra bug, but it's not causing any issue because there aren't imports to these css and scss files. I'm going to keep researching about this ๐Ÿ‘ Thanks to report it

As temp fix, using the flat index, we defined a postbuild script that move the it.html file under it/ and rename it to index.html.

Can you provide me your generated pages structure? (not pages_)

This is with wrapped index

Screenshot 2020-06-09 at 21 20 10

This bug seems related to the index page only. Other first-level pages are working as expected even if declared as a folder.

@equinusocio I prereleased 0.17.2-canary.1, can you confirm if it works now in your case? ๐Ÿ‘

@aralroca Now I get an error while building:

Failed to compile.

./pages/_app.tsx
Module not found: Can't resolve './_app.css' in '/Users/equinusocio/Repositories/site/pages'

That's because pages_/_app.css is missing from the generated pages folder. Maybe any scss, sass, css, pcss files can be just copied and ignored by next-translate?

Also, all of the index folders inside pages are completely empty:

Screenshot 2020-06-09 at 22 09 17

@aralroca Now I get an error while building:

Failed to compile.

./pages/_app.tsx
Module not found: Can't resolve './_app.css' in '/Users/equinusocio/Repositories/site/pages'

That's because pages_/_app.css is missing from the generated pages folder. Maybe any scss, sass, css, pcss files can be just copied and ignored by next-translate?

This is for this PR: https://github.com/vinissimus/next-translate/pull/199/files#diff-36623fc600500d3dce3052db8e970da1R126

But I don't understand why it is given you an error... Do you have some import in your code from /pages instead of pages_?

Perhaps we can change to just copy and not ignore ๐Ÿ‘

Also, all of the index folders inside pages are completely empty:

Yep... index folder is empty, this is because the /index/index.js is moved one level up /index.js, and /somefolder/index/index.js to /somefolder/index.js. Then, inside /somefolder/index.js it has the import to the original pages_/somefolder/index/index.js.

Before I tested using /somefolder/index/index.js and it's not working navigating to /es, so you needed to add /es/index and this is why I moved up.

I'm getting the same issue. All subdirectories with index.js files simply call 404 requests. Bumping this for solidarity.

Hi @aralroca nothing strange in my imports, just some import from modules.

Since this bug occurs only for the index file, I would suggest to just change the output path and the names of the index files for each non-default language, when using a flat index. This will not require people to wrap the index inside a folder (to generate dist/it/index.html)

I've tested by doing manually doing it after the build process. So, with a flat index file, instead of generating this:

dist
โ”œโ”€โ”€ 404.html
โ”œโ”€โ”€ covid-tech.html
โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ it.html
โ”œโ”€โ”€ es.html
โ”œโ”€โ”€ it
โ”‚   โ””โ”€โ”€ covid-tech.html
โ”œโ”€โ”€ es
โ””โ”€โ”€ โ””โ”€โ”€ covid-tech.html

You could generate this (it should work without changing anything else)

dist/
โ”œโ”€โ”€ 404.html
โ”œโ”€โ”€ covid-tech.html
โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ it
โ”‚   โ”œโ”€โ”€ covid-tech.html
โ”‚   โ””โ”€โ”€ index.html
โ”œโ”€โ”€ es
โ”‚   โ”œโ”€โ”€ covid-tech.html
โ””โ”€โ”€ โ””โ”€โ”€ index.html

Each [lang].html index file is moved inside each language folder and renamed to index.html (i think this is also a more common pattern since servers generally look for an index file inside the folder). You can then add a note to the readme to tell people to not wrap the index inside a folder (for the moment), I think it's not a big issue.

This change will also avoid the additional bug here

Since this bug occurs only for the index file, I would suggest to just change the output path and the names of the index files for each non-default language, when using a flat index. This will not require people to wrap the index inside a folder (to generate dist/it/index.html)

@equinusocio In this library, we are not doing anything during next build && next export. The only part is pre-generating the pages folder correctly. Then, the responsible to generate the dist folder is Next.js. Better tell me how you expect the transformation from pages_ to pages, let's see what we can do. But we can't go any further, and perhaps this issue should be reopened: https://github.com/vercel/next.js/issues/6285

I've tested by doing manually doing it after the build process. So, with a flat index file, instead of generating this:

dist
โ”œโ”€โ”€ 404.html
โ”œโ”€โ”€ covid-tech.html
โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ it.html
โ”œโ”€โ”€ es.html
โ”œโ”€โ”€ it
โ”‚   โ””โ”€โ”€ covid-tech.html
โ”œโ”€โ”€ es
โ””โ”€โ”€ โ””โ”€โ”€ covid-tech.html

Apparently this should work without the need to switch to index.html, shouldn't be? I generated a similar structure after next build && next export and it runs without any problem.

I'm getting the same issue. All subdirectories with index.js files simply call 404 requests. Bumping this for solidarity.

@thissell is also happening to you in 0.17.2-canary.1 version?

Yes, but strangely enough I tried working with just a boilerplate next.js server as is and it's giving me the same issue, which is making me think it might be an issue with the latest next edition and not this in specific.

@thissell @equinusocio I see that in Next.js 9.4.5-canary.8 version there is a PR changing this /index/index.js behavior.

Which Next.js version do you have? Can you try a Next.js version >= than 9.4.5-canary.8?

If it's a Next.js issue, maybe we can revert a part of this PR: https://github.com/vinissimus/next-translate/pull/200 to keep the /index/index.js structure again.


@equinusocio I did another pre-release, 0.17.2-canary.2 with the change that you proposed (just copy the css/scss files instead of ignore them).

I tried next 9.4.5-canary.8 + 0.17.1, 0.17.1-canary.1 and 0.17.1-canary.2 with the index folder. The default lang index.html is missing from the dist root:

pages_ and page:

Screenshot 2020-06-10 at 10 25 28

dist
Screenshot 2020-06-10 at 10 23 44

Tried 0.17.2-canary.2 but seem to be running into an issue Error: Cannot find module '../_helpers/getPageNamespaces'

@ay8s it would be very useful if you create a reproducible example ๐Ÿ‘

canary.2 isn't showing that error now, think I just needed more coffee.

I'm using 0.17.2 and next 9.4.5-canary.12 in this branch... https://github.com/sportstimes/f1/tree/feature/static-index but end up with "Error: The provided path / does not match the page: /es.". when navigating to /es

Not sure if this is the same error being discussed in this issue, but sounds related.

@ay8s when I run your project I got an error on utils/generate-calendars.js, but after commenting the line 192 (fs.writeFileSync(path, value)), looks that everything works well. I can navigate from / to /es or other languages without any error...

Weird, was this with the features/static branch @aralroca?

I'm not quite seeing that when running npm run dev locally.

```npm run dev

[email protected] dev /Users/andrewyates/Web/Personal/f1
next-translate && next dev

Building pages | from pages_ to pages
๐Ÿ”จ /generate [ 'common', 'calendar', 'generate' ]
๐Ÿ”จ / [ 'common', 'calendar', 'calendar' ]
๐Ÿ”จ /timezone/[timezone] [ 'common', 'calendar' ]
๐Ÿ”จ /timezones [ 'common', 'calendar', 'timezones' ]
๐Ÿ”จ /year/[year] [ 'common', 'calendar' ]
๐Ÿ”จ /years [ 'common', 'calendar', 'years', 'calendar' ]
ready - started server on http://localhost:3000
Defining routes from exportPathMap
Browserslist: caniuse-lite is outdated. Please run next command npm update
warn - static/development/pages/_app.js is 2.73 MB, and won't be precached. Configure maximumFileSizeToCacheInBytes to change this limit.

static/runtime/main.js is 4.35 MB, and won't be precached. Configure maximumFileSizeToCacheInBytes to change this limit.
info - ready on http://localhost:3000
wait - compiling...
warn - GenerateSW has been called multiple times, perhaps due to running webpack in --watch mode. The precache manifest generated after the first call may be inaccurate! Please see https://github.com/GoogleChrome/workbox/issues/1790 for more information.

static/development/pages/_app.js is 2.73 MB, and won't be precached. Configure maximumFileSizeToCacheInBytes to change this limit.

static/runtime/main.js is 4.35 MB, and won't be precached. Configure maximumFileSizeToCacheInBytes to change this limit.
info - ready on http://localhost:3000
```

Do you happen to have the error itself to hand? If you're using the same branch.

@aralroca I resolved the error with fs.writeFileSync, unfortunately when deploying to Vercel I seem to still get the error "Error: The provided path / does not match the page: /es."

About the error you shared, it looks a memory problem caused by the service worker: GenerateSW has been called multiple times.

This other error Error: The provided path / does not match the page: /es. I don't have an idea. I'm going to try to reproduce it later.

Next 9.5 is out. I will try it with latest next-translate official release

Was this page helpful?
0 / 5 - 0 ratings