Skip to content

fix: getContext() should be sync #222

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
merged 2 commits into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -80,10 +80,10 @@ netlify serve
```
### App Engine Developer Preview usage with Angular@19

If you opt into the App Engine Developer Preview accessing `Request` and `Context` objects is streamlined. Instead of custom Netlify prefixed providers, you should use the standardized injection tokens for those provided by `@angular/ssr` instead:
If you opt into the App Engine Developer Preview accessing `Request` and `Context` objects is streamlined. Instead of custom Netlify prefixed providers, you should use the standardized injection tokens for those provided by `@angular/core` instead:

```diff
+import { REQUEST, REQUEST_CONTEXT } from '@angular/ssr/tokens'
+import { REQUEST, REQUEST_CONTEXT } from '@angular/core'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ref angular/angular-cli#28871 - previous module was removed in @angular/ssr@19.0.0-rc.3

import type { Context } from "@netlify/edge-functions"

export class FooComponent {
2 changes: 1 addition & 1 deletion demo.test.mjs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ test('edge function config', async () => {
'/favicon.ico',
'/heroes/index.html',
'/index.csr.html',
'/main-UP4QHVAQ.js',
'/main-KVCR6MBP.js',
'/polyfills-FFHMD2TL.js',
'/styles-5INURTSO.css',
'/heroes',
1,319 changes: 179 additions & 1,140 deletions demo/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions demo/package.json
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
"@angular/platform-browser-dynamic": "^19.0.0-rc.3",
"@angular/platform-server": "^19.0.0-rc.3",
"@angular/router": "^19.0.0-rc.3",
"@angular/ssr": "^19.0.0-rc.2",
"@angular/ssr": "^19.0.0-rc.3",
"@netlify/angular-runtime": "file:..",
"@netlify/edge-functions": "^2.9.0",
"express": "^4.21.0",
@@ -29,8 +29,8 @@
"zone.js": "~0.15.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^19.0.0-rc.2",
"@angular/cli": "^19.0.0-rc.2",
"@angular-devkit/build-angular": "^19.0.0-rc.3",
"@angular/cli": "^19.0.0-rc.3",
"@angular/compiler-cli": "^19.0.0-rc.3",
"@types/express": "^4.17.21",
"@types/jasmine": "~5.1.0",
6 changes: 3 additions & 3 deletions demo/src/app/dashboard/dashboard.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Inject, OnInit, Optional } from '@angular/core'
import { Component, Inject, OnInit, Optional, REQUEST, REQUEST_CONTEXT } from '@angular/core'
import { Hero } from '../hero'
import { HeroService } from '../hero.service'
import { RouterModule } from '@angular/router'
@@ -16,8 +16,8 @@ export class DashboardComponent implements OnInit {

constructor(
private heroService: HeroService,
@Inject('netlify.request') @Optional() request?: Request,
@Inject('netlify.context') @Optional() context?: Context,
@Inject(REQUEST) @Optional() request?: Request,
@Inject(REQUEST_CONTEXT) @Optional() context?: Context,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to convert that in previous PR (netlify.request -> REQUEST) after setting up AppEngine, so this both moves that and use new place to get REQUEST and REQUEST_CONTEXT tokens from

) {
if (request) {
console.log(
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
"src/**/*.js",
"src/**/*.mjs",
"src/**/*.json",
"src/**/*.d.ts",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise https://github.com/netlify/angular-runtime/blob/main/src/common-engine.d.ts and https://github.com/netlify/angular-runtime/blob/main/src/context.d.ts wouldn't be packed for npm - it worked locally with demo and test fixtures because of package symlinking, but would result in something like below outside of the repo:

Application bundle generation failed. [4.128 seconds]

✘ [ERROR] TS7016: Could not find a declaration file for module '@netlify/angular-runtime/common-engine'. '/Users/misiek/test/angular-19-rnd-1/node_modules/@netlify/angular-runtime/src/common-engine.mjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/netlify__angular-runtime` if it exists or add a new declaration (.d.ts) file containing `declare module '@netlify/angular-runtime/common-engine';` [plugin angular-compiler]

    src/server.ts:2:23:
      2 │ import { render } from '@netlify/angular-runtime/common-engine'
        ╵                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"manifest.yml"
],
"exports": {
2 changes: 1 addition & 1 deletion src/context.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// eslint-disable-next-line no-undef
export const getContext = async () => Netlify?.context
export const getContext = () => (typeof Netlify !== 'undefined' ? Netlify?.context : undefined)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The async was wrong and just result of copying boiler plate from other file. I did miss staging that change in previous PR, but other than that I also had to add check to see if global Netlify is defined, otherwise ng serve (Angular dev mode) would fail with:

ReferenceError: Netlify is not defined
    at Module.getContext (/Users/misiek/test/angular-19-rnd-2/.angular/cache/19.0.0-rc.3/angular-19-rnd-2/vite/deps_ssr/@netlify_angular-runtime_context.js?v=61c1d17e:1:1)
    at netlifyAppEngineHandler (/Users/misiek/test/angular-19-rnd-2/src/server.ts:9:19)
    at /Users/misiek/test/angular-19-rnd-2/node_modules/@angular/build/src/tools/vite/middlewares/ssr-middleware.js:83:38

1,065 changes: 152 additions & 913 deletions tests/fixtures/angular-19-app-engine/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests/fixtures/angular-19-app-engine/package.json
Original file line number Diff line number Diff line change
@@ -20,16 +20,16 @@
"@angular/platform-browser-dynamic": "^19.0.0-rc.3",
"@angular/platform-server": "^19.0.0-rc.3",
"@angular/router": "^19.0.0-rc.3",
"@angular/ssr": "^19.0.0-rc.2",
"@angular/ssr": "^19.0.0-rc.3",
"@netlify/angular-runtime": "file:../../..",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.15.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^19.0.0-rc.2",
"@angular/cli": "^19.0.0-rc.2",
"@angular-devkit/build-angular": "^19.0.0-rc.3",
"@angular/cli": "^19.0.0-rc.3",
"@angular/compiler-cli": "^19.0.0-rc.3",
"@types/express": "^4.17.17",
"@types/jasmine": "~5.1.0",
1,065 changes: 152 additions & 913 deletions tests/fixtures/angular-19-common-engine/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests/fixtures/angular-19-common-engine/package.json
Original file line number Diff line number Diff line change
@@ -20,16 +20,16 @@
"@angular/platform-browser-dynamic": "^19.0.0-rc.3",
"@angular/platform-server": "^19.0.0-rc.3",
"@angular/router": "^19.0.0-rc.3",
"@angular/ssr": "^19.0.0-rc.2",
"@angular/ssr": "^19.0.0-rc.3",
"@netlify/angular-runtime": "file:../../..",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.15.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^19.0.0-rc.2",
"@angular/cli": "^19.0.0-rc.2",
"@angular-devkit/build-angular": "^19.0.0-rc.3",
"@angular/cli": "^19.0.0-rc.3",
"@angular/compiler-cli": "^19.0.0-rc.3",
"@types/express": "^4.17.17",
"@types/jasmine": "~5.1.0",
1,091 changes: 215 additions & 876 deletions tests/fixtures/angular-19-prerender-false/package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/fixtures/angular-19-prerender-false/package.json
Original file line number Diff line number Diff line change
@@ -24,8 +24,8 @@
"zone.js": "~0.15.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^19.0.0-rc.2",
"@angular/cli": "^19.0.0-rc.2",
"@angular-devkit/build-angular": "^19.0.0-rc.3",
"@angular/cli": "^19.0.0-rc.3",
"@angular/compiler-cli": "^19.0.0-rc.3",
"@types/jasmine": "~5.1.0",
"jasmine-core": "~5.4.0",