按照 this Firebase doc 创建了一个 Angular 项目,据我所知,无法为生成的 ssr 函数设置区域。目前我的功能默认为 us-central1(如预期的那样),但我需要将其移动到另一个区域。 ('europe-west3').
要测试/尝试这个问题,只需根据上面的 Firebase 文档生成一个项目。
有没有办法为生成的 ssr 函数表单 angular.json 或任何其他配置文件配置区域? (我不想使用 firebase deploy --only functions --region 'europe-west3' 因为这个项目也是使用 Github Actions 部署的,部署命令在那里是不同的)
我的问题与 AngularFire 包无关,在他们的 github 存储库中有一个文档here),这为不处理 ssr 的函数设置了区域,它只是用于调用部署在 Firebase 上的其他函数。
此外,this不会起作用,因为ssr函数是自动生成的。
我注意到有一个名为 functions.yaml 的文件是在 .firebase/project-name/functions 中自动生成的,其中包含该区域。但是如果我在那个文件中更改它,它会在构建时被覆盖(如预期的那样)。
functions.yaml 看起来像这样(“区域”字段很有趣,但在运行时被覆盖
firebase deploy
):
{
"endpoints": {
"ssrangulartest": {
"platform": "gcfv2",
"region": [
"us-central1"
],
"labels": {},
"httpsTrigger": {},
"entryPoint": "ssr"
}
},
"specVersion": "v1alpha1",
"requiredAPIs": []
}
我还检查了其他堆栈溢出帖子,例如这个,但由于上述原因,我无法在我的 server.ts 文件中以这种方式设置区域(ssr 函数以某种方式自动生成(请尝试生成一个项目使用第一个链接中 firebase 提供的文档)。
为了不浪费您的时间生成整个项目,这是按照 Firebase 文档中的步骤创建的 server.ts 文件:
import 'zone.js/node';
import { APP_BASE_HREF } from '@angular/common';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { existsSync } from 'fs';
import { join } from 'path';
import { AppServerModule } from './src/main.server';
// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
const server = express();
const distFolder = join(process.cwd(), 'dist/hosting/browser');
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/main/modules/express-engine)
server.engine('html', ngExpressEngine({
bootstrap: AppServerModule,
}));
server.set('view engine', 'html');
server.set('views', distFolder);
// Serve static files from /browser
server.get('*.*', express.static(distFolder, {
maxAge: '1y'
}));
// All regular routes use the Universal engine
server.get('*', (req, res) => {
res.set('Cache-Control', 'public, max-age=12000, s-maxage=12000');
res.render(indexHtml, {req, providers: [ {provide: APP_BASE_HREF, useValue: req.baseUrl} ]});
});
return server;
}
function run(): void {
const port = process.env['PORT'] || 4000;
// Start up the Node server
const server = app();
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}
export * from './src/main.server';
我也试过用这样的方式包装 app() :
export const api = functions
.region("europe-west3")
.https.onRequest((request, response) => {
app(request, response);
});
但这也没有用。 (因为 ssr 函数是自动生成的)
注意:这是带有托管的 Web 框架,我不是在谈论为“正常”功能配置功能区域。
使用您提到的配置是不可能的。必须使用提供的 API 在代码中同步定义区域。您也许可以在 index.js 的全局范围内使用 fs.readFileSync() 拉入外部 JSON 文件,解析其内容,并将它们应用于函数构建器。 (请注意,您必须同步执行此操作——您不能使用返回承诺的方法。)
github 中存在一个功能请求,他们说他们计划实施相同的功能
如果上述线程没有帮助,请随时在共享的 github 线程中添加您的疑虑。