summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/orange-dryers-shop.md5
-rw-r--r--packages/astro/src/core/util.ts9
-rw-r--r--packages/astro/test/astro-scripts.test.js30
-rw-r--r--packages/astro/test/fixtures/astro-scripts/deps.mjs2
-rw-r--r--packages/astro/test/fixtures/astro-scripts/src/external-page.astro11
5 files changed, 56 insertions, 1 deletions
diff --git a/.changeset/orange-dryers-shop.md b/.changeset/orange-dryers-shop.md
new file mode 100644
index 000000000..7e12f14ef
--- /dev/null
+++ b/.changeset/orange-dryers-shop.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fix injected scripts not injected to injected routes
diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts
index 11e792b78..7c9ff573a 100644
--- a/packages/astro/src/core/util.ts
+++ b/packages/astro/src/core/util.ts
@@ -110,6 +110,13 @@ function isInPagesDir(file: URL, config: AstroConfig): boolean {
return file.toString().startsWith(pagesDir.toString());
}
+function isInjectedRoute(file: URL, settings: AstroSettings) {
+ for (const route of settings.injectedRoutes) {
+ if(file.toString().endsWith(route.entryPoint)) return true;
+ }
+ return false;
+}
+
function isPublicRoute(file: URL, config: AstroConfig): boolean {
const pagesDir = resolvePages(config);
const parts = file.toString().replace(pagesDir.toString(), '').split('/').slice(1);
@@ -127,7 +134,7 @@ function endsWithPageExt(file: URL, settings: AstroSettings): boolean {
}
export function isPage(file: URL, settings: AstroSettings): boolean {
- if (!isInPagesDir(file, settings.config)) return false;
+ if (!isInPagesDir(file, settings.config) && !isInjectedRoute(file, settings)) return false;
if (!isPublicRoute(file, settings.config)) return false;
return endsWithPageExt(file, settings);
}
diff --git a/packages/astro/test/astro-scripts.test.js b/packages/astro/test/astro-scripts.test.js
index 14b224a69..5420ec0f9 100644
--- a/packages/astro/test/astro-scripts.test.js
+++ b/packages/astro/test/astro-scripts.test.js
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
+import { tailwind } from './fixtures/astro-scripts/deps.mjs';
describe('Scripts (hoisted and not)', () => {
describe('Build', () => {
@@ -139,6 +140,21 @@ describe('Scripts (hoisted and not)', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-scripts/',
+ integrations: [
+ tailwind(),
+ {
+ name: 'test-script-injection-with-injected-route',
+ hooks: {
+ 'astro:config:setup': ({ injectRoute, injectScript }) => {
+ injectScript(
+ 'page',
+ `import '/src/scripts/something.js';`
+ );
+ injectRoute({ pattern: 'injected-route', entryPoint: 'src/external-page.astro' });
+ },
+ },
+ }
+ ],
vite: {
build: {
assetsInlineLimit: 0,
@@ -182,5 +198,19 @@ describe('Scripts (hoisted and not)', () => {
});
expect(found).to.equal(1);
});
+
+ it('Injected scripts are injected to injected routes', async () => {
+ let res = await fixture.fetch('/injected-route');
+ let html = await res.text();
+ let $ = cheerio.load(html);
+ let found = 0;
+ let moduleScripts = $('[type=module]');
+ moduleScripts.each((i, el) => {
+ if ($(el).attr('src').includes('@id/astro:scripts/page.js')) {
+ found++;
+ }
+ });
+ expect(found).to.equal(1);
+ });
});
});
diff --git a/packages/astro/test/fixtures/astro-scripts/deps.mjs b/packages/astro/test/fixtures/astro-scripts/deps.mjs
new file mode 100644
index 000000000..e6a090e7b
--- /dev/null
+++ b/packages/astro/test/fixtures/astro-scripts/deps.mjs
@@ -0,0 +1,2 @@
+export { default as tailwind } from '@astrojs/tailwind';
+
diff --git a/packages/astro/test/fixtures/astro-scripts/src/external-page.astro b/packages/astro/test/fixtures/astro-scripts/src/external-page.astro
new file mode 100644
index 000000000..8d8a49cff
--- /dev/null
+++ b/packages/astro/test/fixtures/astro-scripts/src/external-page.astro
@@ -0,0 +1,11 @@
+---
+---
+
+<html lang="en">
+<head>
+ <title>External page</title>
+</head>
+<body>
+ <h1>My external page</h1>
+</body>
+</html>