Skip to content

Commit

Permalink
Avoid removing leading slash for build.assetsPrefix (#6969)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Phillips <matthew@skypack.dev>
  • Loading branch information
bluwy and matthewp authored May 2, 2023
1 parent b5482ce commit 77270cc
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
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('/');
}

0 comments on commit 77270cc

Please sign in to comment.