Fix double serving of files in component server
Now the server can serve from the tests directory, it will sometimes get
two requests for the same file, one prefixed with front_end and the
other not. If that happens the server now redirects the request such
that we only ever serve each file once and the browser doesn't
double-execute a module.
Change-Id: I14b44aad8b16d1c3f2f787082c70532a8f8776e9
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2599746
Auto-Submit: Jack Franklin <jacktfranklin@chromium.org>
Commit-Queue: Paul Lewis <aerotwist@chromium.org>
Reviewed-by: Paul Lewis <aerotwist@chromium.org>
diff --git a/scripts/component_server/server.js b/scripts/component_server/server.js
index 32978a9..83b8b40 100644
--- a/scripts/component_server/server.js
+++ b/scripts/component_server/server.js
@@ -198,6 +198,26 @@
respondWithHtml(response, newFileContents);
} else {
+ if (filePath.startsWith('/front_end')) {
+ /**
+ * We load files from the test directory whose paths will often start with
+ * /front_end if they load in frontend code. However we also get requests
+ * from within the front_end directory which do not start with /front_end.
+ * This means we might get two requests for the same file:
+ *
+ * 1) /front_end/ui/ui.js
+ * 2) /ui/ui.js
+ *
+ * If we serve them both it will mean we load ui/ui.js twice. So instead
+ * we redirect permanently to the non-/front_end prefixed URL so that the
+ * browser only loads each module once.
+ */
+ response.writeHead(301, {
+ Location: filePath.replace('/front_end', ''),
+ });
+ response.end();
+ return;
+ }
// This means it's an asset like a JS file or an image.
const normalizedPath = normalizeImagePathIfRequired(filePath);
@@ -215,8 +235,7 @@
* right place.
*/
const fileIsInTestFolder = normalizedPath.startsWith('/test/');
- const fileStartsWithFrontEnd = normalizedPath.startsWith('/front_end/');
- if (fileIsInTestFolder || fileStartsWithFrontEnd) {
+ if (fileIsInTestFolder) {
fullPath = path.join(devtoolsFrontendFolder, '..', normalizedPath);
}