app-backend: fix config injection for index path

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-11-20 11:30:49 +01:00
parent f435b653d3
commit 74c3f2ac58
3 changed files with 39 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-app-backend': patch
---
Fixed a bug where config would not be injected on the `/` and `/index.html` paths.
@@ -75,6 +75,7 @@ describe('appPlugin', () => {
'package.json': '{}',
dist: {
static: {},
'index.html': '<html><head></head></html>',
'index.html.tmpl': '<html><head></head></html>',
},
});
@@ -92,6 +93,26 @@ describe('appPlugin', () => {
],
});
const rootContent = await fetch(`http://localhost:${server.port()}`).then(
res => res.text(),
);
expect(rootContent).toBe(`<html><head>
<script type="backstage.io/config">
[]
</script>
</head></html>`);
const indexContent = await fetch(
`http://localhost:${server.port()}/index.html`,
).then(res => res.text());
expect(indexContent).toBe(`<html><head>
<script type="backstage.io/config">
[]
</script>
</head></html>`);
const htmlContent = await fetch(
`http://localhost:${server.port()}/api/app/some/html5/route`,
).then(res => res.text());
+13 -1
View File
@@ -302,7 +302,17 @@ async function createEntryPointRouter({
staticRouter.use(notFoundHandler());
router.use('/static', staticRouter);
router.use(
const rootRouter = Router();
rootRouter.use((req, _res, next) => {
// Make sure / and /index.html are handled by the HTML5 route below
if (req.url === '/' || req.url === '/index.html') {
next('router');
} else {
next();
}
});
rootRouter.use(
express.static(rootDir, {
setHeaders: (res, path) => {
// The Cache-Control header instructs the browser to not cache html files since it might
@@ -315,7 +325,9 @@ async function createEntryPointRouter({
},
}),
);
router.use(rootRouter);
// HTML5 routing
router.get('/*', (_req, res) => {
if (injectResult?.indexHtmlContent) {
res.setHeader('Content-Type', 'text/html; charset=utf-8');