Skip to content

Commit ca34279

Browse files
authored
Merge pull request #261 from webpack-contrib/relative-links-to-assets
Use relative links for serving internal assets
2 parents 3ce1b8c + 99818f9 commit ca34279

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ _Note: Gaps between patch versions are faulty, broken or test releases._
1515
<!-- Add changelog entries for new changes under this section -->
1616

1717
* **Improvements**
18+
* Use relative links for serving internal assets ([#261](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/261), fixes [#254](https://github.com/webpack-contrib/webpack-bundle-analyzer/issues/254))
1819
* Properly escape embedded JS/JSON ([#262](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/262))
1920

2021
* **Bug Fix**

src/viewer.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const Logger = require('./Logger');
1414
const analyzer = require('./analyzer');
1515

1616
const projectRoot = path.resolve(__dirname, '..');
17+
const assetsRoot = path.join(projectRoot, 'public');
1718

1819
module.exports = {
1920
startServer,
@@ -169,7 +170,13 @@ async function generateReport(bundleStats, opts) {
169170
}
170171

171172
function getAssetContent(filename) {
172-
return fs.readFileSync(`${projectRoot}/public/${filename}`, 'utf8');
173+
const assetPath = path.join(assetsRoot, filename);
174+
175+
if (!assetPath.startsWith(assetsRoot)) {
176+
throw new Error(`"${filename}" is outside of the assets root`);
177+
}
178+
179+
return fs.readFileSync(assetPath, 'utf8');
173180
}
174181

175182
/**

views/script.ejs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
<%- escapeScript(assetContent(filename)) %>
55
</script>
66
<% } else { %>
7-
<script src="/<%= filename %>"></script>
7+
<script src="<%= filename %>"></script>
88
<% } %>

0 commit comments

Comments
 (0)