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

fix(astro): prerendering issue when path contains underscore #11243

Merged
merged 5 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/honest-ravens-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes a prerendering issue for libraries in `node_modules` when a folder with an underscore is in the path.
16 changes: 14 additions & 2 deletions packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,23 @@ function isInjectedRoute(file: URL, settings: AstroSettings) {
}

function isPublicRoute(file: URL, config: AstroConfig): boolean {
const pagesDir = resolvePages(config);
const parts = file.toString().replace(pagesDir.toString(), '').split('/').slice(1);
const rootDir = config.root.toString();
const pagesDir = resolvePages(config).toString();
const fileDir = file.toString();

// Normalize the file directory path by removing the pagesDir prefix if it exists,
// otherwise remove the rootDir prefix.
Comment on lines +127 to +128
Copy link
Member

@ematipico ematipico Jun 14, 2024

Choose a reason for hiding this comment

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

The comment should also explain why this is necessary. The code itself isn't too complicated to understand, so a comment that explains it seems superfluous. However, it's worth adding an explanation of the motive because if you left a comment here, it means there's a particular case to handle :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might happen that a page is located in node_modules, and the Astro project itself is in a path containing the _ character. In such cases, we should use a relative path to bypass the segment containing _.

WDYT?

const normalizedDir = fileDir.startsWith(pagesDir) ? fileDir.slice(pagesDir.length) : fileDir.slice(rootDir.length);

const parts = normalizedDir
.replace(pagesDir.toString(), '')
.split('/')
.slice(1);

for (const part of parts) {
if (part.startsWith('_')) return false;
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'astro/config';
import fakeIntegration from 'fake-astro-library';

export default defineConfig({
integrations: [
fakeIntegration(),
],
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>Project with underscore in the folder name</title>
</head>
<body>
<h1>Testing</h1>
<script>
console.log('hi');
</script>
</body>
</html>
25 changes: 25 additions & 0 deletions packages/astro/test/underscore-in-folder-name.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';

describe('Projects with a underscore in the folder name', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/_underscore in folder name/',
output: 'hybrid',
adapter: testAdapter(),
});
await fixture.build();
});

it('includes page from node_modules/fake-astro-library', async () => {
const app = await fixture.loadTestAdapterApp();
/** @type {Set<string>} */
const assets = app.manifest.assets;
assert.equal(assets.has('/index.html'), true);
assert.equal(assets.has('/404.html'), true);
});
});
Loading