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

Remove problematic chars from asset filenames #44

Merged
merged 1 commit into from
Dec 15, 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
1 change: 1 addition & 0 deletions __playwright-tests__/fixtures/asset-paths.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<div class="with-rewritten-background-img"></div>
<div class="with-rewritten-background-img-inline"></div>
<div style="background: url('/img?url=fixtures/pink.png'); no-repeat center;"></div>
<div><img src="/img/another%Cwith%Cpercents"/></div>
</div>

<h2>srcset</h2>
Expand Down
4 changes: 4 additions & 0 deletions __playwright-tests__/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ app.get('/img/another', (req, res) => {
res.sendFile(path.join(__dirname, 'fixtures/pink.png'));
});

app.get('/img/another%Cwith%Cpercents', (req, res) => {
res.sendFile(path.join(__dirname, 'fixtures/pink.png'));
});

app.get('/asset-paths/relative/purple.png', (req, res) => {
res.sendFile(path.join(__dirname, 'fixtures/purple.png'));
});
Expand Down
2 changes: 1 addition & 1 deletion src/write-archive/archive-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('ArchiveFile', () => {

const filePath = archiveFile.toFileSystemPath();

expect(filePath).toEqual('/localhost%3A9999/some/directory/hi.png');
expect(filePath).toEqual('/localhost3A9999/some/directory/hi.png');
});
});

Expand Down
7 changes: 7 additions & 0 deletions src/write-archive/archive-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { ArchiveResponse, UrlString } from '../resource-archive';
* - encoding the query string into the file name to avoid overwriting files
* that have a different response due to the query string
* - truncating file names and path parts that are too big for some file systems
* - removing problematic characters
* - ensuring the file name has an extension
*/
export class ArchiveFile {
Expand Down Expand Up @@ -43,6 +44,7 @@ export class ArchiveFile {
sanitizedSrc = this.ensureNonDirectory(sanitizedSrc);
sanitizedSrc = this.encodeQueryString(sanitizedSrc);
sanitizedSrc = this.truncateFileName(sanitizedSrc);
sanitizedSrc = this.removeSpecialChars(sanitizedSrc);
sanitizedSrc = this.addExtension(sanitizedSrc);

return sanitizedSrc;
Expand Down Expand Up @@ -80,6 +82,11 @@ export class ArchiveFile {
return `${pathname.startsWith('/') ? '/' : ''}${path.join(...rebuiltPieces)}`;
}

private removeSpecialChars(pathname: string) {
// The storybook server seems to have a problem with percents in file names
return pathname.replace(/[%]/g, '');
}

private addExtension(pathname: string) {
if ('error' in this.response || !this.response.contentType) return pathname;

Expand Down
Loading