-
-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access-Control-Allow-Origin is not working when streaming the data #259
Comments
See the test i added to the PR #260 on how to do it correct. |
Any workaround? upd: Use custom preHandler hook: fastify.addHook('preHandler', corsPreHandler)
export const corsPreHandler = (req: FastifyRequest, res: FastifyReply, done: HookHandlerDoneFunction) => {
const blacklistedPaths = ['/page/:pageParam']
if (!blacklistedPaths.includes(req.routerPath)) {
const origin = req.headers.origin ?? ''
res.header('Access-Control-Allow-Origin', origin)
res.header('Access-Control-Allow-Methods', '*')
res.header('Access-Control-Allow-Headers', '*')
}
const isPreflight = /options/i.test(req.method)
if (isPreflight) {
return res.send()
}
done()
} taken from SO |
Why cant you use reply.send(stream)? |
|
Also worked when I did this:
|
I believe nothing it can do on our side. |
Prerequisites
Fastify version
8.2.0
Plugin version
No response
Node.js version
18.16.1
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
Window 10
Description
I set up the cors like this:
server.register(cors, { origin: '*', });
It works for normal APIs, but when I stream the data, it does not work.
const stream = fs.createReadStream(filePath); reply.type('application/json').header('Content-Length', fs.statSync(filePath).size); return stream.pipe(reply.raw);
Steps to Reproduce
Follow the description!
Expected Behavior
It should set "Access-Control-Allow-Origin" to "*" for the response header!
The text was updated successfully, but these errors were encountered: