- I have my Angular application that is hosted at www.A.com,
- My customer has a web site that is hosted at www.B.com
- I need to let my customer to insert the Angular app on his webpage without copying application files to his website. I want my customer to just add some HTML code that references the files from my original www.A.com website.
So, at some page www.B.com/somepage.html
customer would put something like that:
<base href="https://www.A.com" />
<app-root></app-root>
<script src="runtime.469fa17746b90e8a757c.js" defer></script>
<script src="polyfills.35a5ca1855eb057f016a.js" defer></script>
<script src="scripts.3cf90f8fc92130239739.js" defer></script>
<script src="main.f229882672708596d13b.js" defer></script>
I tried this approach but encountered with a whole bunch of issues related to CORS and CORB.
Does anyone have a clear path on how a webpage can use Angular app from another domain?
EDIT NOTE 1:
solution with iframe does not work for us because it negatively affects SEO (Google penalizes us for it), so we cannot use iframe with external link on our website.
EDIT NOTE 2:
I have ownership of www.A.com (so I can adjust any configuration/HTTP headers required for my app to be used from external websites)
5
Answers
We can Do it in multiple ways
By using Angular Elements you can package Angular component as Web Component, a web standard for defining new HTML elements in a framework-agnostic way.
https://angular.io/guide/elements
Were you compiling the application with the –base-href flag pointing to the client application domain?
In this way, the application will be based on the client’s domain, and maybe(i never try before) you can access http://www.B.com/%5Bruta that you define in angular routing as input to the app}
You can’t make a functional page or element by putting some lines of Angular client-side rendered codes on the fly. They are meaningful for the Angular app. In case of successful injection regarding default Angular configurations, the main script’s hash changes every time you build the project.
Best way regarding
iframe
avoidance is creating a web component using angular so that you can later use that element anywhere by just importing a single file. In this way, this is recommended to reuse simple components, not a whole functional page for a perfect working and secure (prevent phishing) project. There’s a good and brief sample Using Angular component in Non-Angular App. More professional considerations using web components listed inside Why I don’t use web components which I agree with Rich Harris.This is where the concept of micro-front-ends come into picture. This is the exact scenario.
Now this can be done in n number of ways. One of which is Angular Elements.
Angular provides you an option to make your application into a web-element, which can be included into another index.html file as a main.js file.
You can read more about it here:
How to make angular elements: https://angular.io/guide/elements
What is micro-front-end and how to use it:
Method 1) Use a framework called SPA (single page application)
you can read about it here: https://single-spa.js.org/docs/microfrontends-concept
Example: