【问题标题】:How to correctly deploy an Angular Universal website to a specific path?如何正确地将 Angular Universal 网站部署到特定路径?
【发布时间】:2021-05-13 17:44:10
【问题描述】:

因此,我有一个应用程序设置为通过 bitbuckets 管道部署到特定路径,例如“website.com/path”,但是当我访问该站点时,它会尝试加载根路径“website.com”中的文件。 com/scripts.js”,所以我必须在服务器上 ssh 并将 index.html 上的“base”从“/”更改为“/path/”,但是手动这样做是非常多余的,甚至 SSR 也是无法正常工作。

P.S 该站点最初部署在主根路径“website.com”上,但一位高级开发人员刚刚告诉我“您的网站现在位于 'website.com/path' 上,然后将管道上的路径更改为 '/ path/' ”,我做到了。

bitbucket 管道:

      - npm install
      - npm run build:ssr:stage
      - scp -o StrictHostKeyChecking=no -r dist/sns-angular xxxx@15x.xxx.xx.xx:/home/sco/www/website.com/new/sns-angular-new
      - ssh -o StrictHostKeyChecking=no xxxx@15x.xxx.xx.xx "cd /home/sco/www/website.com/new  && rm -rf sns-angular && mv sns-angular-new sns-angular"

脚本:

  "scripts": {
    "ng": "ng",
    "start": "ng run sns-angular:serve-ssr",
    "build": "ng build",
    "build:ssr:stage": "ng build --deploy-url /sns-angular/ --base-href / && ng run sns-angular:server:stage",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "dev:ssr": "ng run sns-angular:serve-ssr",
    "serve:ssr": "node dist/sns-angular/server/main.js",
    "build:ssr": "ng build --prod && ng run sns-angular:server:production",
    "prerender": "ng run sns-angular:prerender"
  }```

【问题讨论】:

    标签: javascript angular deployment bitbucket devops


    【解决方案1】:

    要在构建时设置基础,您必须将 --base-href 中的 build:ssr:stage 脚本调整为例如--base-href /my-path.

    要让 SSR 工作,您必须使用 build:ssr 脚本构建您的应用程序(不要忘记在此处添加您的 --base-href)。构建完成后,您应该能够使用serve:ssr 脚本启动服务器,该脚本默认侦听端口 4200。

    【讨论】:

    • 如何指定资产?我使用了 deploy-url /myPath/ 但它不起作用我收到如下错误:Unable to locate stylesheet: /var/www/html/dist/browser/myPath/styles.16d89a3cd69123f2ba9c.css 因为我无法正确设置资产文件夹...
    • 您可以将基本 href 添加到您的环境中,并使用此路径为资产添加前缀。在构建时,您可以调整此路径,具体取决于您使用的环境。你能提供一个示例回购吗?在这方面为您提供帮助会更容易。
    猜你喜欢
    • 1970-01-01
    • 2018-08-08
    • 2019-04-28
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2020-08-29
    相关资源
    最近更新 更多