This answer describes how to handle large assets in dev + production (using angular 12+) using GCP, but you can easily adapt this answer for other clouds or environments.
1st - Setup Angular to serve local assets
Angular Documentation reference
This assume you have some large or localhost only assets under src/large-assets. We're using angular object syntax to make them available under assets/
(Also make sure src/large-assets is gitignored, or under git-lfs)
"projects": {
"[your-project-name]": {
"architect": {
"build": {
"configurations": {
"production": {
"assets": [
"src/favicon.ico",
"src/assets"
],
"development": {
"assets": [
"src/favicon.ico",
"src/assets",
{
"glob": "**/*",
"input": "src/large-assets/",
"output": "/assets/"
}
2nd - Make assets URL environment specific
# src/environments/environment.ts
export const environment = {
...
assetsUrl: 'https://localhost:4200/assets/',
...
};
# src/environments/environment.prod.ts
const GCP_PROJECT_NAME = 'gcp-project-name';
export const environment = {
...
assetsUrl: `https://storage.googleapis.com/${GCP_PROJECT_NAME}.appspot.com/assets/`,
...
};
3rd - Setup Angular to replace environment in prod
# angular.json
"projects": {
"[your-project-name]": {
"architect": {
"build": {
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
4th - Use in components
# src/components/mycomponent.ts
import { environment } from './environments/environment';
# src/components/mycomponent.html
<img [src]="environment.assetsUrl + 'images/myimage.jpeg'" alt="alt">
5th - Upload your local large assets to CDN for production use
This is an example using GCP
gsutil cp -r src/large-assets gs://${GCP_PROJECT_NAME}.appspot.com/
gsutil iam ch allUsers:objectViewer gs://${GCP_PROJECT_NAME}.appspot.com/