Skip to content

Commit 8d84b30

Browse files
authored
bug: Support remote url for css in content collection (withastro#15254)
fixes: withastro#15252
1 parent 3da6272 commit 8d84b30

File tree

9 files changed

+56
-3
lines changed

9 files changed

+56
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes CSS `assetsPrefix` with remote URLs incorrectly prepending a forward slash
6+
7+
When using `build.assetsPrefix` with a remote URL (e.g., `https://cdn.example.com`) for CSS assets, the generated `<link>` elements were incorrectly getting a `/` prepended to the full URL, resulting in invalid URLs like `/https://cdn.example.com/assets/style.css`.
8+
9+
This fix checks if the stylesheet link is a remote URL before prepending the forward slash.

packages/astro/src/content/runtime.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ZodIssueCode, z } from 'zod';
66
import type { GetImageResult, ImageMetadata } from '../assets/types.js';
77
import { imageSrcToImportId } from '../assets/utils/resolveImports.js';
88
import { AstroError, AstroErrorData } from '../core/errors/index.js';
9-
import { prependForwardSlash } from '../core/path.js';
9+
import { isRemotePath, prependForwardSlash } from '../core/path.js';
1010
import {
1111
type AstroComponentFactory,
1212
createComponent,
@@ -821,7 +821,7 @@ async function render({
821821
.map((link: any) => {
822822
return renderUniqueStylesheet(result, {
823823
type: 'external',
824-
src: prependForwardSlash(link),
824+
src: isRemotePath(link) ? link : prependForwardSlash(link),
825825
});
826826
})
827827
.join('');

packages/astro/test/astro-assets-prefix.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ describe('Assets Prefix - Static', () => {
6565
const imgAsset = $('img');
6666
assert.match(imgAsset.attr('src'), assetsPrefixRegex);
6767
});
68+
69+
it('MDX content collection CSS imports should start with assetsPrefix', async () => {
70+
const html = await fixture.readFile('/mdx-blog/index.html');
71+
const $ = cheerio.load(html);
72+
const stylesheets = $('link[rel="stylesheet"]');
73+
assert.ok(stylesheets.length > 0, 'Expected at least one stylesheet');
74+
stylesheets.each((_i, el) => {
75+
assert.match(el.attribs.href, assetsPrefixRegex);
76+
});
77+
});
6878
});
6979

7080
describe('Assets Prefix - with path prefix', () => {

packages/astro/test/fixtures/astro-assets-prefix/astro.config.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import mdx from '@astrojs/mdx';
12
import react from '@astrojs/react';
23
import { defineConfig } from 'astro/config';
34
import { testImageService } from '../../test-image-service.js';
@@ -6,9 +7,10 @@ import { testImageService } from '../../test-image-service.js';
67
export default defineConfig({
78
// test custom base to make sure things work
89
base: '/custom-base',
9-
integrations: [react()],
10+
integrations: [react(), mdx()],
1011
build: {
1112
assetsPrefix: 'http://localhost:4321',
13+
inlineStylesheets: 'never',
1214
},
1315
image: {
1416
service: testImageService(),

packages/astro/test/fixtures/astro-assets-prefix/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.0.0",
44
"private": true,
55
"dependencies": {
6+
"@astrojs/mdx": "workspace:*",
67
"@astrojs/react": "workspace:*",
78
"astro": "workspace:*",
89
"react": "^18.3.1",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: MDX Post with CSS
3+
cover: ../../assets/penguin1.jpg
4+
---
5+
6+
import './styles.css';
7+
8+
# Hello from MDX
9+
10+
This MDX file imports a CSS file to test assetsPrefix with content collections.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: red;
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
import { getCollection } from "astro:content";
3+
const posts = await getCollection("blog");
4+
const mdxPost = posts.find(p => p.id === 'mdx-with-css.mdx');
5+
const { Content } = await mdxPost.render();
6+
---
7+
8+
<html>
9+
<head>
10+
<title>MDX Blog</title>
11+
</head>
12+
<body>
13+
<Content />
14+
</body>
15+
</html>

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)