Skip to content

Conversation

@vzkn
Copy link
Contributor

@vzkn vzkn commented Feb 12, 2025

🔗 Linked issue

Resolves #27722

📚 Description

When using Nuxt version 3.11 or above, pre-rendering pages with multilingual slugs (including non-ASCII characters) causes filenames to be encoded to US-ASCII. This encoding significantly increases the filename length, potentially exceeding the file system's 255-character limit, and ultimately triggers an "ENAMETOOLONG" error. This issue did not occur in version 3.10 and earlier.

To address this, I reviewed all the commits between v3.10.3...v3.11.0. One commit in particular stood out: in bc44dfc, @danielroe updated the nitro.options._config.storage configuration for the pre-render cache (internal:nuxt:prerender).

Before:

nitro.options._config.storage['internal:nuxt:prerender'] = { driver: 'memory' }

After:

nitro.options._config.storage = defu(nitro.options._config.storage, {
  'internal:nuxt:prerender': {
    driver: pathToFileURL(await resolvePath(join(distDir, 'core/runtime/nitro/cache-driver'))).href,
    base: resolve(nuxt.options.buildDir, 'cache/nitro/prerender')
  }
})

Previously, the pre-render cache was stored in memory. With the updated configuration, the cache is now stored using a cache-driver in the <rootDir>/.nuxt/cache/nitro/prerender folder.

Upon further inspection of the cache-driver code, I discovered that when a pathname includes non-ASCII characters, it is used directly as a filename. This can lead to the "ENAMETOOLONG" error when the filename exceeds the 255-character limit. To resolve this, I added a call to decodeURIComponent to decode the pathnames before they are used, ensuring that filenames remain within acceptable length limits.

@vzkn vzkn requested a review from danielroe as a code owner February 12, 2025 22:01
@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@vzkn vzkn changed the title fix(nitro): decode URI components in cache driver methods fix(nuxt): decode URI components in cache driver methods Feb 12, 2025
@coderabbitai
Copy link

coderabbitai bot commented Feb 12, 2025

Walkthrough

The changes update the key processing in the cache-driver module by modifying the normalizeFsKey function to include a decoding step for URI components. The function now applies decodeURIComponent to the input string before replacing colons with underscores. This adjustment affects the handling of keys in the setItem, hasItem, and getItem methods, ensuring that any encoded characters in the keys are decoded prior to normalisation. The new implementation changes the way keys are prepared for file system operations and interactions with the LRU cache, enhancing the accuracy of key management within the module.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e6bd73e and 5b32065.

📒 Files selected for processing (1)
  • packages/nuxt/src/core/runtime/nitro/cache-driver.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/nuxt/src/core/runtime/nitro/cache-driver.js
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: codeql (javascript-typescript)
  • GitHub Check: build
  • GitHub Check: code

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/nuxt/src/core/runtime/nitro/cache-driver.js (1)

21-26: Consider adding error handling for decodeURIComponent.

Whilst the implementation is correct, decodeURIComponent can throw URIError for malformed input.

Consider wrapping the decode operation in a try-catch:

 async setItem (key, value, opts) {
+  let decodedKey
+  try {
+    decodedKey = decodeURIComponent(key)
+  } catch (err) {
+    decodedKey = key
+  }
   await Promise.all([
-    fs.setItem?.(normalizeFsKey(decodeURIComponent(key)), value, opts),
+    fs.setItem?.(normalizeFsKey(decodedKey), value, opts),
     lru.setItem?.(key, value, opts),
   ])
 },
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0b80b58 and e6bd73e.

📒 Files selected for processing (1)
  • packages/nuxt/src/core/runtime/nitro/cache-driver.js (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (20)
  • GitHub Check: test-fixtures (ubuntu-latest, built, webpack, default, manifest-on, json, 18)
  • GitHub Check: test-fixtures (ubuntu-latest, built, webpack, async, manifest-on, json, 18)
  • GitHub Check: test-fixtures (ubuntu-latest, built, rspack, default, manifest-on, json, 18)
  • GitHub Check: test-fixtures (ubuntu-latest, built, rspack, async, manifest-on, json, 18)
  • GitHub Check: test-fixtures (ubuntu-latest, built, vite, default, manifest-off, json, 18)
  • GitHub Check: test-fixtures (ubuntu-latest, built, vite, default, manifest-on, json, 18)
  • GitHub Check: test-fixtures (ubuntu-latest, built, vite, async, manifest-off, json, 18)
  • GitHub Check: test-fixtures (ubuntu-latest, built, vite, async, manifest-on, js, 18)
  • GitHub Check: test-fixtures (ubuntu-latest, built, vite, async, manifest-on, json, 18)
  • GitHub Check: test-fixtures (ubuntu-latest, dev, vite, default, manifest-off, json, 18)
  • GitHub Check: typecheck (windows-latest, node)
  • GitHub Check: typecheck (windows-latest, bundler)
  • GitHub Check: test-fixtures (ubuntu-latest, dev, vite, async, manifest-off, json, 18)
  • GitHub Check: typecheck (ubuntu-latest, node)
  • GitHub Check: test-fixtures (ubuntu-latest, dev, vite, async, manifest-on, js, 18)
  • GitHub Check: typecheck (ubuntu-latest, bundler)
  • GitHub Check: release-pr
  • GitHub Check: test-benchmark
  • GitHub Check: test-size
  • GitHub Check: code
🔇 Additional comments (1)
packages/nuxt/src/core/runtime/nitro/cache-driver.js (1)

23-23: LGTM! The changes effectively address the multilingual slug issue.

The addition of decodeURIComponent before normalising the file system keys ensures proper handling of encoded characters, preventing potential "ENAMETOOLONG" errors with multilingual slugs. The implementation maintains consistency across all file system operations whilst preserving the original keys in the LRU cache.

Let's verify the changes handle various edge cases correctly:

Also applies to: 28-28, 31-31

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Description: Test the changes with various multilingual and special characters

# Test 1: Check for any existing error handling for decodeURIComponent
rg -A 2 "try.*decodeURIComponent" .

# Test 2: Look for similar patterns in other parts of the codebase
rg -A 2 "normalizeFsKey.*decodeURIComponent" .

Length of output: 978


Verification Complete – Cache-Driver Methods Appropriately Handle Encoded Keys

The grep output confirms that the decodeURIComponent is correctly applied in all relevant methods (setItem, hasItem, and getItem). This ensures that the file system keys are properly decoded before normalisation, effectively addressing issues with multilingual slugs.

  • setItem (Line 23): Uses normalizeFsKey(decodeURIComponent(key)) for file system storage.
  • hasItem (Line 28): Checks the existence using normalizeFsKey(decodeURIComponent(key)).
  • getItem (Line 31): Retrieves the item via normalizeFsKey(decodeURIComponent(key)).

All changes are consistent and align with the PR objectives.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 12, 2025

Open in Stackblitz

@nuxt/kit

npm i https://pkg.pr.new/@nuxt/kit@30973

@nuxt/rspack-builder

npm i https://pkg.pr.new/@nuxt/rspack-builder@30973

nuxt

npm i https://pkg.pr.new/nuxt@30973

@nuxt/schema

npm i https://pkg.pr.new/@nuxt/schema@30973

@nuxt/vite-builder

npm i https://pkg.pr.new/@nuxt/vite-builder@30973

@nuxt/webpack-builder

npm i https://pkg.pr.new/@nuxt/webpack-builder@30973

commit: 5b32065

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 12, 2025

CodSpeed Performance Report

Merging #30973 will not alter performance

Comparing Vahagn-Zaqaryan:fix/nitro-prerender-filename-encoding (5b32065) with main (0b80b58)

Summary

✅ 9 untouched benchmarks

@vzkn vzkn requested a review from danielroe February 12, 2025 22:31
* @param {string} item
*/
const normalizeFsKey = item => item.replaceAll(':', '_')
const normalizeFsKey = item => decodeURIComponent(item.replaceAll(':', '_'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my only worry is whether windows/mac/linux filesystems will all support non-ASCII characters. (maybe, if not, we need an additional sanitisation step)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a really good point, thanks for the feedback!

I’ve given this a lot of thought, and here’s my reasoning:

  1. Initial Approach:
    I initially considered that users might supply virtually any string for the article slug. Since we can’t predict which characters they might use, I thought about normalizing the input by applying a hashing function (e.g., SHA-256 or a lighter alternative). This would ensure that the output is consistent and only contains allowed characters.

  2. Reevaluation:
    After looking at the bigger picture, I realized that the files created by the cache-driver during the nuxt generate command are temporary, they’re only used for generation. What really matters is that in the final output, under the .output folder, users see a sub-folder with the original name they expect.

For example:

  • During Generation:
    <rootDir>/.nuxt/cache/nitro/prerender
    Cache Folder

  • Final Output:
    <rootDir>/.output/public
    Output Folder

Let me know if you see any issues with this approach or if there’s another angle I might have missed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what approach are you suggesting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we stick with the decodeURIComponent approach to avoid any performance overhead from hashing. Since the temporary cache file and the final output folder share the same name, any system that doesn't support non-ASCII characters will fail to create both. If I implement a solution for the temporary cache file, then it will fail when creating the folder.

@vzkn vzkn requested a review from danielroe February 20, 2025 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error "ENAMETOOLONG: name too long" occurs during pre-rendering on versions 3.11 and 3.12

2 participants