这是一段代码:
const Announce = ({ additionalHeaders, err, slug, announceRaw }) => {
console.log(additionalHeaders)
if(err) return <Errors err=err/>
return (
<>
//component content
</>
)
}
export async function getServerSideProps (ctx) {
const { slug } = ctx.query;
const additionalHeaders = { Cookie: ctx.req.headers['cookie'] };
try {
const data = await AnnounceService.getAnnounceBySlugSSR(slug, additionalHeaders);
return {
props: {
additionalHeaders,
data
},
};
} catch (err) {
return {
props: {
additionalHeaders
err
},
};
}
}
在这里我在服务器端调用我的API路由,如果提供了令牌cookie,它将返回json数据,否则返回403状态代码。
在本地开发过程中一切正常=>提供了其他标头
我可以通过浏览器请求或Postman来获取我的API,并且一切运行正常
但是我无法在Firebase生产服务器上获得Cookie标头
PS:我在credentials : include
上使用CORS,但这绝对不是问题所在
{
"additionalHeaders": {
"Cookie": "_ga=GA1.3.90XXXXXXXXXXX; _gid=GA1.3.211XXXXXXXX; _gat=1; token=MY_AUTH_TOKEN_PASSED_TO_API, someOthersCookies : ...."
}
}
{
"additionalHeaders": {}
}
PS2: 我尝试使用Vercel(Zeit)进行部署,并且在生产模式下运行时很吸引人
如果使用Firebase无法解决此问题,我将必须订阅Vercel企业计划
有人知道吗?
谢谢
functions.js
'use strict';
const admin = require('firebase-admin');
const functions = require('firebase-functions');
const next = require('next');
const runtimeOpts = {
timeoutSeconds: 300,
memory: '1GB',
};
admin.initializeApp();
const dev = process.env.NODE_ENV !== 'production';
const app = next({
dev,
conf: {
distDir: 'dist',
},
});
const handle = app.getRequestHandler();
exports.next = functions
.runWith(runtimeOpts)
.https
.onRequest((req, res) => {
try {
app.prepare().then(() => handle(req, res));
} catch (err) {
return err;
}
});
firebase.json
{
"functions": {
"source": ".",
"ignore": [
"node_modules",
".git",
".gitignore",
".nyc_output",
"firebase-debug.log"
]
},
"hosting": {
"public": "public",
"rewrites": [
{
"source": "**/**",
"function": "next"
}
],
"ignore": [
".firebase/**",
"firebase.json",
".firebaserc",
"**/node_modules/**",
"**/.*",
"**"
]
}
}
next.config.js
const withPlugins = require('next-compose-plugins')
const withImages = require('next-images')
const path = require('path')
require('dotenv').config()
const nextConfig = {
distDir: 'dist',
env: {},
webpack: (config) => {
config.resolve.mainFields = ['main', 'browser', 'module']
return config
},
}
module.exports = nextConfig
package.json
{
"main": "functions.js",
"scripts": {
"clean": "rimraf dist",
"dev": "yarn clean && next",
"start": "next start",
"build": "rimraf dist && next build",
"deploy": "yarn build && yarn push",
"ignore-engine": "yarn config set ignore-engines true",
"push": "yarn ignore-engine && firebase deploy --only functions,hosting --debug"
},
"engines": {
"node": "10" // required relating to firebase functions documentation (bypassed with ignore-engine command)
},
"dependencies": {
"next": "9.3.5",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.0",
.....
}