Skip to content
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

fix(http): force macro task creation during request #49546

Closed
wants to merge 5 commits into from

Conversation

alan-agius4
Copy link
Contributor

@alan-agius4 alan-agius4 commented Mar 22, 2023

refactor(platform-server): deprecate useAbsoluteUrl and baseUrl

These two options where created for a feature which was never completed. angular/universal#1860

Eventually these options should be added to withTransferCache HTTP logic.

DEPRECATED: PlatformConfig.baseUrl and PlatformConfig.useAbsoluteUrl platform-server config options are deprecated as these were not used.


fix(http): force macro task creation during HTTP request

This commit adds a background macrotask when an XHR request is performed. The macrotask is started during loadstart and ended during loadend event.

The macrotask is needed so that the application is not stabilized during HTTP calls. This is important for server rendering, as the application is rendering when the application is stabilized.
The application is stabilized when there are no longer pending Macro and Micro tasks intercepted by Zone.js, Since an XHR request is none of these, we create a background macrotask so that Zone.js is
made aware that there is something pending.

Prior to this change, we patched the HttpHandler in @angular/platform-server but this is not enough, as there can be multiple HttpHandler in an application, example when importing HttpClient in a lazy loaded component/module.
Which causes a new unpatched instance of HttpHandler to be created in the child injector which is not intercepted by Zone.js and thus the application is stabalized and rendered before the XHR request is finalized.

NB: Zone.js is fundamental for SSR and currently, it's not possible to do SSR without it.

Closes: #49425

@alan-agius4 alan-agius4 force-pushed the http-fix-universal branch 2 times, most recently from 8bd7c3b to 0814906 Compare March 23, 2023 10:13
@angular-robot angular-robot bot added the detected: deprecation PR contains a commit with a deprecation label Mar 23, 2023
@alan-agius4 alan-agius4 added area: server Issues related to server-side rendering area: common/http target: major This PR is targeted for the next major release labels Mar 23, 2023
@ngbot ngbot bot modified the milestone: Backlog Mar 23, 2023
@alan-agius4 alan-agius4 marked this pull request as ready for review March 23, 2023 12:10
@alan-agius4 alan-agius4 added the action: review The PR is still awaiting reviews from at least one requested reviewer label Mar 23, 2023
@alan-agius4
Copy link
Contributor Author

//cc @AndrewKushnir, not sure if you want to review this, as we discussed about this a bit yesterday.

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

@alan-agius4 looks great 👍

We'd probably need a TGP for this change.

packages/common/http/src/xhr.ts Outdated Show resolved Hide resolved
packages/common/http/src/xhr.ts Outdated Show resolved Hide resolved
@@ -320,3 +337,20 @@ export class HttpXhrBackend implements HttpBackend {
});
}
}

const MAX_INT = 2147483647;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use Number.MAX_VALUE here? If not, could you plz add a comment into the code, so we have this context in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added comment why.

Copy link
Contributor

Choose a reason for hiding this comment

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

What about Number.MAX_SAFE_INTEGER? That's basically what it is for, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, Number.MAX_SAFE_INTEGER is a 53 bit integer.

@Injectable()
export class ServerXhr implements XhrFactory {
build(): XMLHttpRequest {
return new xhr2.XMLHttpRequest();
}
}

export abstract class ZoneMacroTaskWrapper<S, R> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Love the removal of this code (in favor of a more simple solution!) 👍

packages/platform-server/src/tokens.ts Show resolved Hide resolved
packages/platform-server/src/tokens.ts Show resolved Hide resolved
@pullapprove pullapprove bot requested a review from dylhunn March 27, 2023 22:32
@AndrewKushnir AndrewKushnir added the action: global presubmit The PR is in need of a google3 global presubmit label Mar 27, 2023
Copy link
Contributor

@jessicajaniuk jessicajaniuk left a comment

Choose a reason for hiding this comment

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

reviewed-for: fw-http, fw-platform-server, integration-tests, public-api

This commit adds a background macrotask when an XHR request is performed. The macrotask is started during `loadstart` and ended during `loadend` event.

The macrotask is needed so that the application is not stabilized during HTTP calls. This is important for server rendering, as the application is rendering when the application is stabilized.
The application is stabilized when there are no longer pending Macro and Micro tasks intercepted by Zone.js, Since an XHR request is none of these, we create a background macrotask so that Zone.js is
made aware that there is something pending.

Prior to this change, we patched the `HttpHandler` in `@angular/platform-server` but this is not enough, as there can be multiple `HttpHandler` in an application, example when importing `HttpClient` in a lazy loaded component/module.
Which causes a new unpatched instance of `HttpHandler` to be created in the child injector which is not intercepted by Zone.js and thus the application is stabalized and rendered before the XHR request is finalized.

NB: Zone.js is fundamental for SSR and currently, it's not possible to do SSR without it.

Closes: angular#49425
These two options where created for a feature which was never completed. angular/universal#1860

Eventually these options should be added to `withTransferCache` HTTP logic.

DEPRECATED: `PlatformConfig.baseUrl` and `PlatformConfig.useAbsoluteUrl` platform-server config options  are deprecated as these were not used.
@alan-agius4
Copy link
Contributor Author

@alan-agius4 alan-agius4 added merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note and removed action: global presubmit The PR is in need of a google3 global presubmit labels Mar 28, 2023
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Reviewed-for: public-api

@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Mar 28, 2023
@alan-agius4 alan-agius4 removed the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Mar 29, 2023
@atscott
Copy link
Contributor

atscott commented Mar 29, 2023

This PR was merged into the repository by commit e994608.

@atscott atscott closed this in 45a6ac0 Mar 29, 2023
atscott pushed a commit that referenced this pull request Mar 29, 2023
…49546)

These two options where created for a feature which was never completed. angular/universal#1860

Eventually these options should be added to `withTransferCache` HTTP logic.

DEPRECATED: `PlatformConfig.baseUrl` and `PlatformConfig.useAbsoluteUrl` platform-server config options  are deprecated as these were not used.

PR Close #49546
@alan-agius4 alan-agius4 deleted the http-fix-universal branch March 29, 2023 17:25
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: common/http area: server Issues related to server-side rendering detected: deprecation PR contains a commit with a deprecation target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The TransferState doesn't render the data script after a HTTP Request
5 participants