-
Notifications
You must be signed in to change notification settings - Fork 14
Closed
Labels
Description
Describe the bug
Update my server.ts file to manage 301 redirect and added sanitization for json in final souuce code but both changes are not working, i can see its working locally but after deploying to netlify its not working
To Reproduce
Steps to reproduce the behavior:
- Make 301 redirects in server.ts file
function app(): express.Express {
const server = express();
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
const browserDistFolder = resolve(serverDistFolder, '../browser');
const indexHtml = join(serverDistFolder, 'index.server.html');
const commonEngine = new CommonEngine();
server.set('view engine', 'html');
server.set('views', browserDistFolder);
server.get('*.*', express.static(browserDistFolder, {
maxAge: '1y'
}));
server.get('*', (req, res, next) => {
const { protocol, originalUrl, baseUrl, headers } = req;
const redirectUrl = redirectMap[req.url];
if (redirectUrl) {
res.redirect(301, redirectUrl);
}else{
commonEngine
.render({
bootstrap,
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: browserDistFolder,
providers: [
{ provide: APP_BASE_HREF, useValue: baseUrl },
],
})
.then((html) => {
// Extract the state
const stateRegex = /<script id="ng-state" type="application\/json">(.*?)<\/script>/;
const match = html.match(stateRegex);
if (match && match[1]) {
const state = JSON.parse(match[1]);
const sanitizedState = sanitizeState(state);
const serializedState = JSON.stringify(sanitizedState);
// Replace the original state with the sanitized state
html = html.replace(stateRegex, `<script id="ng-state" type="application/json">${serializedState}</script>`);
}
res.send(html);
})
.catch((err) => next(err));
}
});
return server;
}
function run(): void {
const port = process.env['PORT'] || 4000;
const server = app();
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
run();
redirectMap:
export const redirectMap: { [key: string]: string } = {
'old url 1' : 'new url 1',
'old url 2' : 'new url 2',
'old url 3' : 'new url 3',
// other urls
}
sanitize-state :
export function sanitizeState(state: any): any {
const sanitizedState = JSON.parse(JSON.stringify(state));
Object.keys(sanitizedState).forEach(key => {
if (sanitizedState[key]?.u) {
delete sanitizedState[key].u;
}
});
return sanitizedState;
}
Expected behavior
after deployment redirect should work but its not working and same for sanitize state
Versions
- Angular.js: 18
If you're using the CLI to build
N/A
If you're using file-based installation
[build]
command = "npm run build"
publish = "dist/dapps/browser"
[[plugins]]
package="@netlify/angular-runtime"
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
reason for using redirect in server.ts because redirect is not supported in SSR and i have to redirect more than 3000 urls