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

Avoid removing leading slash for build.assetsPrefix #6969

Merged
merged 2 commits into from
May 2, 2023
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
6 changes: 6 additions & 0 deletions .changeset/rotten-worms-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astrojs/image': patch
'astro': patch
---

Avoid removing leading slash for `build.assetsPrefix` value in the build output
13 changes: 12 additions & 1 deletion packages/astro/src/core/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ function isString(path: unknown): path is string {
}

export function joinPaths(...paths: (string | undefined)[]) {
return paths.filter(isString).map(trimSlashes).join('/');
return paths
.filter(isString)
.map((path, i) => {
if (i === 0) {
return removeTrailingForwardSlash(path);
} else if (i === paths.length - 1) {
return removeLeadingForwardSlash(path);
} else {
return trimSlashes(path);
}
})
.join('/');
}

export function removeFileExtension(path: string) {
Expand Down
52 changes: 52 additions & 0 deletions packages/astro/test/astro-assets-prefix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ describe('Assets Prefix - Static', () => {
});
});

describe('Assets Prefix - Static with path prefix', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-assets-prefix/',
build: {
assetsPrefix: '/starting-slash',
},
});
await fixture.build();
});

it('all stylesheets should start with assetPrefix', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const stylesheets = $('link[rel="stylesheet"]');
stylesheets.each((i, el) => {
expect(el.attribs.href).to.match(/^\/starting-slash\/.*/);
});
});
});

describe('Assets Prefix - Server', () => {
let app;

Expand Down Expand Up @@ -119,3 +142,32 @@ describe('Assets Prefix - Server', () => {
expect(imgAsset.attr('src')).to.match(assetsPrefixRegex);
});
});

describe('Assets Prefix - Server with path prefix', () => {
let app;

before(async () => {
const fixture = await loadFixture({
root: './fixtures/astro-assets-prefix/',
output: 'server',
adapter: testAdapter(),
build: {
assetsPrefix: '/starting-slash',
},
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});

it('all stylesheets should start with assetPrefix', async () => {
const request = new Request('http://example.com/custom-base/');
const response = await app.render(request);
expect(response.status).to.equal(200);
const html = await response.text();
const $ = cheerio.load(html);
const stylesheets = $('link[rel="stylesheet"]');
stylesheets.each((i, el) => {
expect(el.attribs.href).to.match(/^\/starting-slash\/.*/);
});
});
});
13 changes: 12 additions & 1 deletion packages/integrations/image/src/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,16 @@ function isString(path: unknown): path is string {
}

export function joinPaths(...paths: (string | undefined)[]) {
return paths.filter(isString).map(trimSlashes).join('/');
return paths
.filter(isString)
.map((path, i) => {
if (i === 0) {
return removeTrailingForwardSlash(path);
} else if (i === paths.length - 1) {
return removeLeadingForwardSlash(path);
} else {
return trimSlashes(path);
}
})
.join('/');
}