Skip to content

Commit 8ec1a7e

Browse files
committed
[INTERNAL] Only replace versions in files the build would process as well
1 parent 1ee6723 commit 8ec1a7e

File tree

7 files changed

+51
-9
lines changed

7 files changed

+51
-9
lines changed

lib/middleware/serveResources.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const etag = require("etag");
66
const fresh = require("fresh");
77
const parseurl = require("parseurl");
88

9-
const rProperties = /\.properties$/;
9+
const rProperties = /\.properties$/i;
10+
const rReplaceVersion = /\.(library|js|json)$/i;
1011

1112
function isFresh(req, res) {
1213
return fresh(req.headers, {
@@ -35,12 +36,13 @@ function createMiddleware({resourceCollections}) {
3536

3637
let type;
3738
let charset;
38-
if (rProperties.test(resource.getPath())) {
39+
const resourcePath = resource.getPath();
40+
if (rProperties.test(resourcePath)) {
3941
// Special handling for *.properties files which are encoded with charset ISO-8859-1.
4042
type = "text/plain";
4143
charset = "ISO-8859-1";
4244
} else {
43-
type = mime.lookup(resource.getPath()) || "application/octet-stream";
45+
type = mime.lookup(resourcePath) || "application/octet-stream";
4446
}
4547

4648
if (!res.getHeader("Content-Type")) {
@@ -63,7 +65,11 @@ function createMiddleware({resourceCollections}) {
6365

6466
let stream = resource.getStream();
6567

66-
if (charset === "UTF-8" && (type.startsWith("text/") || type === "application/javascript")) {
68+
// Only execute version replacement for UTF-8 encoded resources because replaceStream will always output
69+
// UTF-8 anyways.
70+
// Also, only process .library, *.js and *.json files. Just like it's done in Application-
71+
// and LibraryBuilder
72+
if (charset === "UTF-8" && rReplaceVersion.test(resourcePath)) {
6773
if (resource._project) {
6874
stream = stream.pipe(replaceStream("${version}", resource._project.version));
6975
} else {

test/fixtures/application.a/webapp/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<title>Application A - Version ${version}</title>
4+
<title>Application A</title>
55
</head>
66
<body>
77

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Not replaced: ${version}</title>
5+
</head>
6+
<body>
7+
8+
</body>
9+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(`${version}`);

test/lib/server/acceptRemoteConnections.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ test("Get resource from application.a (/index.html) with enabled remote connecti
4141
}
4242
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
4343
t.regex(res.headers["content-type"], /html/, "Correct content type");
44-
t.regex(res.text, /<title>Application A - Version 1.0.0<\/title>/, "Correct response");
44+
t.regex(res.text, /<title>Application A<\/title>/, "Correct response");
4545
});
4646
});

test/lib/server/h2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ test("Get resource from application.a (/index.html)", (t) => {
5454
}
5555
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
5656
t.regex(res.headers["content-type"], /html/, "Correct content type");
57-
t.regex(res.text, /<title>Application A - Version 1.0.0<\/title>/, "Correct response");
57+
t.regex(res.text, /<title>Application A<\/title>/, "Correct response");
5858
});
5959
});

test/lib/server/main.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,30 @@ test("Get resource from application.a (/index.html)", (t) => {
4040
}
4141
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
4242
t.regex(res.headers["content-type"], /html/, "Correct content type");
43-
t.regex(res.text, /<title>Application A - Version 1.0.0<\/title>/, "Correct response");
43+
t.regex(res.text, /<title>Application A<\/title>/, "Correct response");
44+
});
45+
});
46+
47+
48+
test("Get resource from application.a with not replaced version placeholder(/versionTest.html)", (t) => {
49+
return request.get("/versionTest.html").then((res) => {
50+
if (res.error) {
51+
t.fail(res.error.text);
52+
}
53+
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
54+
t.regex(res.headers["content-type"], /html/, "Correct content type");
55+
t.regex(res.text, /<title>Not replaced: \${version}<\/title>/, "Correct response");
56+
});
57+
});
58+
59+
test("Get resource from application.a with replaced version placeholder (/versionTest.js)", (t) => {
60+
return request.get("/versionTest.js").then((res) => {
61+
if (res.error) {
62+
t.fail(res.error.text);
63+
}
64+
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
65+
t.regex(res.headers["content-type"], /application\/javascript/, "Correct content type");
66+
t.deepEqual(res.text, "console.log(`1.0.0`);\n", "Correct response");
4467
});
4568
});
4669

@@ -119,6 +142,9 @@ test("Get app_pages from discovery middleware (/discovery/app_pages)", (t) => {
119142
"app_pages": [
120143
{
121144
"entry": "index.html"
145+
},
146+
{
147+
"entry": "versionTest.html"
122148
}
123149
]
124150
}, "Correct response");
@@ -480,7 +506,7 @@ test("Get index of resources", (t) => {
480506
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
481507
t.is(res.headers["content-type"], "text/html", "Correct content type");
482508
t.is(/<title>(.*)<\/title>/i.exec(res.text)[1], "Index of /", "Found correct title");
483-
t.deepEqual(res.text.match(/<td/g).length, 30, "Found correct amount of <td> elements");
509+
t.deepEqual(res.text.match(/<td/g).length, 42, "Found correct amount of <td> elements");
484510
}),
485511
request.get("/resources").then((res) => {
486512
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");

0 commit comments

Comments
 (0)