-
Notifications
You must be signed in to change notification settings - Fork 11.9k
fix(@angular/build): resolve assets correctly during i18n prerendering #32714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+140
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
tests/e2e/tests/build/server-rendering/server-routes-output-mode-static-i18n-http-calls.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import assert, { match } from 'node:assert'; | ||
| import { join } from 'node:path'; | ||
| import { readFile, writeMultipleFiles } from '../../../utils/fs'; | ||
| import { ng, noSilentNg, silentNg } from '../../../utils/process'; | ||
| import { getGlobalVariable } from '../../../utils/env'; | ||
| import { installWorkspacePackages, uninstallPackage } from '../../../utils/packages'; | ||
| import { useSha } from '../../../utils/project'; | ||
| import { langTranslations, setupI18nConfig } from '../../i18n/setup'; | ||
|
|
||
| export default async function () { | ||
| assert( | ||
| getGlobalVariable('argv')['esbuild'], | ||
| 'This test should not be called in the Webpack suite.', | ||
| ); | ||
|
|
||
| // Setup project | ||
| await setupI18nConfig(); | ||
|
|
||
| // Forcibly remove in case another test doesn't clean itself up. | ||
| await uninstallPackage('@angular/ssr'); | ||
| await ng('add', '@angular/ssr', '--skip-confirmation', '--skip-install'); | ||
| await useSha(); | ||
| await installWorkspacePackages(); | ||
|
|
||
| await writeMultipleFiles({ | ||
| // Add asset | ||
| 'public/media.json': JSON.stringify({ dataFromAssets: true }), | ||
| // Update component to do an HTTP call to asset and API. | ||
| 'src/app/app.ts': ` | ||
| import { ChangeDetectorRef, Component, inject } from '@angular/core'; | ||
| import { JsonPipe } from '@angular/common'; | ||
| import { RouterOutlet } from '@angular/router'; | ||
| import { HttpClient } from '@angular/common/http'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-root', | ||
| imports: [JsonPipe, RouterOutlet], | ||
| template: \` | ||
| <p>{{ assetsData | json }}</p> | ||
| <p>{{ apiData | json }}</p> | ||
| <router-outlet></router-outlet> | ||
| \`, | ||
| }) | ||
| export class App { | ||
| assetsData: any; | ||
| apiData: any; | ||
| private readonly cdr: ChangeDetectorRef = inject(ChangeDetectorRef); | ||
|
|
||
| constructor() { | ||
| const http = inject(HttpClient); | ||
|
|
||
| http.get('media.json').toPromise().then((d) => { | ||
| this.assetsData = d; | ||
| this.cdr.markForCheck(); | ||
| }); | ||
|
|
||
| http.get('/api').toPromise().then((d) => { | ||
| this.apiData = d; | ||
| this.cdr.markForCheck(); | ||
| }); | ||
| } | ||
| } | ||
| `, | ||
| // Add http client and route | ||
| 'src/app/app.config.ts': ` | ||
| import { ApplicationConfig } from '@angular/core'; | ||
| import { provideRouter } from '@angular/router'; | ||
|
|
||
| import { Home } from './home/home'; | ||
| import { provideClientHydration } from '@angular/platform-browser'; | ||
| import { provideHttpClient, withFetch } from '@angular/common/http'; | ||
|
|
||
| export const appConfig: ApplicationConfig = { | ||
| providers: [ | ||
| provideRouter([{ | ||
| path: 'home', | ||
| component: Home, | ||
| }]), | ||
| provideClientHydration(), | ||
| provideHttpClient(withFetch()), | ||
| ], | ||
| }; | ||
| `, | ||
| 'src/server.ts': ` | ||
| import { AngularNodeAppEngine, writeResponseToNodeResponse, isMainModule, createNodeRequestHandler } from '@angular/ssr/node'; | ||
| import express from 'express'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| export function app(): express.Express { | ||
| const server = express(); | ||
| const browserDistFolder = join(import.meta.dirname, '../browser'); | ||
| const angularNodeAppEngine = new AngularNodeAppEngine(); | ||
|
|
||
| server.get('/api', (req, res) => { | ||
| res.json({ dataFromAPI: true }) | ||
| }); | ||
|
|
||
| server.use(express.static(browserDistFolder, { | ||
| maxAge: '1y', | ||
| index: 'index.html' | ||
| })); | ||
|
|
||
| server.use((req, res, next) => { | ||
| angularNodeAppEngine.handle(req) | ||
| .then((response) => response ? writeResponseToNodeResponse(response, res) : next()) | ||
| .catch(next); | ||
| }); | ||
| return server; | ||
| } | ||
|
|
||
| const server = app(); | ||
|
|
||
| if (isMainModule(import.meta.url)) { | ||
| const port = process.env['PORT'] || 4000; | ||
| server.listen(port, (error) => { | ||
| if (error) { | ||
| throw error; | ||
| } | ||
| console.log(\`Node Express server listening on http://localhost:\${port}\`); | ||
| }); | ||
| } | ||
|
|
||
| export const reqHandler = createNodeRequestHandler(server); | ||
| `, | ||
| }); | ||
|
|
||
| await silentNg('generate', 'component', 'home'); | ||
|
|
||
| await noSilentNg('build', '--output-mode=static'); | ||
|
|
||
| for (const { lang, outputPath } of langTranslations) { | ||
| const contents = await readFile(join(outputPath, 'home/index.html')); | ||
| match(contents, /<p>{[\S\s]*"dataFromAssets":[\s\S]*true[\S\s]*}<\/p>/); | ||
alan-agius4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| match(contents, /<p>{[\S\s]*"dataFromAPI":[\s\S]*true[\S\s]*}<\/p>/); | ||
alan-agius4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| match(contents, new RegExp(`<base href="\\/${lang}\\/">`)); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.