| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e6ccc74 commit 45e498f
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,6 +26,7 @@ import { | |||
| 26 | 26 | WritableSerializableRouteTreeNode, | |
| 27 | 27 | } from './models'; | |
| 28 | 28 | import type { RenderWorkerData } from './render-worker'; | |
| 29 | + import { generateRedirectStaticPage } from './utils'; | ||
| 29 | 30 | ||
| 30 | 31 | type PrerenderOptions = NormalizedApplicationBuildOptions['prerenderOptions']; | |
| 31 | 32 | type AppShellOptions = NormalizedApplicationBuildOptions['appShellOptions']; | |
@@ -380,28 +381,3 @@ function addTrailingSlash(url: string): string { | |||
| 380 | 381 | function removeLeadingSlash(value: string): string { | |
| 381 | 382 | return value[0] === '/' ? value.slice(1) : value; | |
| 382 | 383 | } | |
| 383 | - | ||
| 384 | - /** | ||
| 385 | - * Generates a static HTML page with a meta refresh tag to redirect the user to a specified URL. | ||
| 386 | - * | ||
| 387 | - * This function creates a simple HTML page that performs a redirect using a meta tag. | ||
| 388 | - * It includes a fallback link in case the meta-refresh doesn't work. | ||
| 389 | - * | ||
| 390 | - * @param url - The URL to which the page should redirect. | ||
| 391 | - * @returns The HTML content of the static redirect page. | ||
| 392 | - */ | ||
| 393 | - function generateRedirectStaticPage(url: string): string { | ||
| 394 | - return ` | ||
| 395 | - <!DOCTYPE html> | ||
| 396 | - <html> | ||
| 397 | - <head> | ||
| 398 | - <meta charset="utf-8"> | ||
| 399 | - <title>Redirecting</title> | ||
| 400 | - <meta http-equiv="refresh" content="0; url=${url}"> | ||
| 401 | - </head> | ||
| 402 | - <body> | ||
| 403 | - <pre>Redirecting to <a href="${url}">${url}</a></pre> | ||
| 404 | - </body> | ||
| 405 | - </html> | ||
| 406 | - `.trim(); | ||
| 407 | - } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ import type { ESMInMemoryFileLoaderWorkerData } from './esm-in-memory-loader/loa | |||
| 12 | 12 | import { patchFetchToLoadInMemoryAssets } from './fetch-patch'; | |
| 13 | 13 | import { DEFAULT_URL, launchServer } from './launch-server'; | |
| 14 | 14 | import { loadEsmModuleFromMemory } from './load-esm-from-memory'; | |
| 15 | + import { generateRedirectStaticPage } from './utils'; | ||
| 15 | 16 | ||
| 16 | 17 | export interface RenderWorkerData extends ESMInMemoryFileLoaderWorkerData { | |
| 17 | 18 | assetFiles: Record</** Destination */ string, /** Source */ string>; | |
@@ -48,7 +49,13 @@ async function renderPage({ url }: RenderOptions): Promise<string | null> { | |||
| 48 | 49 | new Request(new URL(url, serverURL), { signal: AbortSignal.timeout(30_000) }), | |
| 49 | 50 | ); | |
| 50 | 51 | ||
| 51 | - return response ? response.text() : null; | ||
| 52 | + if (!response) { | ||
| 53 | + return null; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + const location = response.headers.get('Location'); | ||
| 57 | + | ||
| 58 | + return location ? generateRedirectStaticPage(location) : response.text(); | ||
| 52 | 59 | } | |
| 53 | 60 | ||
| 54 | 61 | async function initialize() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,3 +19,28 @@ export function isSsrRequestHandler( | |||
| 19 | 19 | ): value is ReturnType<typeof createRequestHandler> { | |
| 20 | 20 | return typeof value === 'function' && '__ng_request_handler__' in value; | |
| 21 | 21 | } | |
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * Generates a static HTML page with a meta refresh tag to redirect the user to a specified URL. | ||
| 25 | + * | ||
| 26 | + * This function creates a simple HTML page that performs a redirect using a meta tag. | ||
| 27 | + * It includes a fallback link in case the meta-refresh doesn't work. | ||
| 28 | + * | ||
| 29 | + * @param url - The URL to which the page should redirect. | ||
| 30 | + * @returns The HTML content of the static redirect page. | ||
| 31 | + */ | ||
| 32 | + export function generateRedirectStaticPage(url: string): string { | ||
| 33 | + return ` | ||
| 34 | + <!DOCTYPE html> | ||
| 35 | + <html> | ||
| 36 | + <head> | ||
| 37 | + <meta charset="utf-8"> | ||
| 38 | + <title>Redirecting</title> | ||
| 39 | + <meta http-equiv="refresh" content="0; url=${url}"> | ||
| 40 | + </head> | ||
| 41 | + <body> | ||
| 42 | + <pre>Redirecting to <a href="${url}">${url}</a></pre> | ||
| 43 | + </body> | ||
| 44 | + </html> | ||
| 45 | + `.trim(); | ||
| 46 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,8 @@ export default async function () { | |||
| 29 | 29 | await writeFile( | |
| 30 | 30 | 'src/app/app.routes.ts', | |
| 31 | 31 | ` | |
| 32 | - import { Routes } from '@angular/router'; | ||
| 32 | + import { inject } from '@angular/core'; | ||
| 33 | + import { Routes, Router } from '@angular/router'; | ||
| 33 | 34 | import { Home } from './home/home'; | |
| 34 | 35 | import { Ssg } from './ssg/ssg'; | |
| 35 | 36 | import { SsgWithParams } from './ssg-with-params/ssg-with-params'; | |
@@ -47,6 +48,12 @@ export default async function () { | |||
| 47 | 48 | path: 'ssg-redirect', | |
| 48 | 49 | redirectTo: 'ssg' | |
| 49 | 50 | }, | |
| 51 | + { | ||
| 52 | + path: 'ssg-redirect-via-guard', | ||
| 53 | + canActivate: [() => { | ||
| 54 | + return inject(Router).createUrlTree(['ssg'], { queryParams: { foo: 'bar' }}) | ||
| 55 | + }], | ||
| 56 | + }, | ||
| 50 | 57 | { | |
| 51 | 58 | path: 'ssg/:id', | |
| 52 | 59 | component: SsgWithParams, | |
@@ -106,8 +113,10 @@ export default async function () { | |||
| 106 | 113 | 'ssg/index.html': /ng-server-context="ssg".+ssg works!/, | |
| 107 | 114 | 'ssg/one/index.html': /ng-server-context="ssg".+ssg-with-params works!/, | |
| 108 | 115 | 'ssg/two/index.html': /ng-server-context="ssg".+ssg-with-params works!/, | |
| 109 | - // When static redirects as generated as meta tags. | ||
| 116 | + // When static redirects are generated as meta tags. | ||
| 110 | 117 | 'ssg-redirect/index.html': '<meta http-equiv="refresh" content="0; url=/ssg">', | |
| 118 | + 'ssg-redirect-via-guard/index.html': | ||
| 119 | + '<meta http-equiv="refresh" content="0; url=/ssg?foo=bar">', | ||
| 111 | 120 | }; | |
| 112 | 121 | ||
| 113 | 122 | for (const [filePath, fileMatch] of Object.entries(expects)) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments