razshare / sveltekit-sse

Server Sent Events with SvelteKit
https://www.npmjs.com/package/sveltekit-sse
MIT License
307 stars 9 forks source link

Cross-Origin Request Blocked: #11

Closed elhananjair closed 1 year ago

elhananjair commented 1 year ago

Hello @tncrazvan After the update I am getting this error on firefox:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 200.

elhananjair commented 1 year ago

It seems it works with other paths like /localhost based on the way I am using the package and your demo here

razshare commented 1 year ago

Hi @elhananjair , The project you mentioned works just fine for me on firefox.

Peek 2023-10-25 22-51

The errors you see are standard behaviour for some browsers due to page reloading (you're essentially closing the sse stream without notice when you close a tab).

I tested this on firefox version Mozilla Firefox 118.0.2.

Your issue seems to be related http headers.

Your configuration is either missing some required headers for your setup or your disallowing some origins in your http response.

The event itself allows you to set specific http headers, so you could do something like this

/** src/lib/server/longrunningtask.js */

import { event } from 'sveltekit-sse'

export function long_running_task() {
  return event(async emit => {
    //...
    emit('some data...')
    //...
  })
  .setHeader("Access-Control-Allow-Origin", "*")
  .toResponse()
}

You probably shouldn't allow * in a production environment though.

There's not much else I can help you with unless you reproduce the error in a repository where I can debug the project.

elhananjair commented 1 year ago

Yeah with .setHeader("Access-Control-Allow-Origin", "*") it works fine.