Skip to content
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

Error Pages with streamed responses always reporting as 200. #12987

Open
bertybot opened this issue Nov 11, 2024 · 5 comments
Open

Error Pages with streamed responses always reporting as 200. #12987

bertybot opened this issue Nov 11, 2024 · 5 comments

Comments

@bertybot
Copy link
Contributor

Describe the bug

When creating a page with streamed responses in the load, whether it is in a parent layout or the page itself, the page will always return a 200 regardless of errors thrown.

Error pages will still function and report $page.status correctly but in the network tab and response returned from handle the status will be reported as 200 which causes a bunch of weird side effects in your code when using things like preloadData .

Reproduction

https://sveltelab.dev/f6fb6ey520ivhjy

Please notice that while the error page renders correctly with the right code, the hook reporting the status logs 200 and the network tab shows 200.

Logs

No response

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 1.28 GB / 7.47 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.13.1 - ~/.nvm/versions/node/v20.13.1/bin/node
    npm: 10.5.2 - ~/.nvm/versions/node/v20.13.1/bin/npm
    pnpm: 9.6.0 - ~/.nvm/versions/node/v20.13.1/bin/pnpm
  npmPackages:
    @sveltejs/adapter-node: ^5.2.9 => 5.2.9 
    @sveltejs/kit: ^2.8.0 => 2.8.0 
    @sveltejs/vite-plugin-svelte: ^4.0.0 => 4.0.0 
    @sveltejs/vite-plugin-svelte-inspector: ^3.0.1 => 3.0.1 
    svelte: ^5.1.4 => 5.1.4 
    vite: ^5.4.10 => 5.4.10

Severity

serious, but I can work around it

Additional Information

No response

@eltigerchino
Copy link
Member

eltigerchino commented Nov 12, 2024

This is how SvelteKit's client-side routing works. The load function response received from the server will always be 200 but the actual response body itself contains the real status code:

{
    "type": "data",
    "nodes": [
        {
            "type": "skip"
        },
        {
            "type": "skip"
        },
        {
            "type": "error",
            "error": {
                "message": "Bugged Reports as 200"
            },
            "status": 500
        }
    ]
}

If you disable CSR, you'll see the 500 status codes being logged in the network tab and hook.

There's also currently a PR to allow preloadData to detect load functions that errored but it won't come until SvelteKit 3.0 due to a breaking change. #12579

@eltigerchino eltigerchino closed this as not planned Won't fix, can't repro, duplicate, stale Nov 12, 2024
@bertybot
Copy link
Contributor Author

bertybot commented Nov 12, 2024

This is how SvelteKit's client-side routing works. The load function response received from the server will always be 200 but the actual response body itself contains the real status code:

{
    "type": "data",
    "nodes": [
        {
            "type": "skip"
        },
        {
            "type": "skip"
        },
        {
            "type": "error",
            "error": {
                "message": "Bugged Reports as 200"
            },
            "status": 500
        }
    ]
}

If you disable CSR, you'll see the 500 status codes being logged in the network tab and hook.

There's also currently a PR to allow preloadData to detect load functions that errored but it won't come until SvelteKit 3.0 due to a breaking change. #12579

Sorry I originally found this bug while trying to debug preloadData, and conflated my two problems thinking one was causing the other since they are so similar.

This is happening on SSR as well. If I hit this page from postman it will report back as 200 despite being 500. Currently all of my 404 pages are reporting back as 200. Look at the console.log from the server hook. nonBugged page reports the response correctly as 500, but the bugged one says 200 with the only difference between the 2 being a promise that returns nothing. This has been causing me a ton of problems with my telemetry as a lot of my errors cannot be found on the server and instead showing as ok responses. Also, I would think this would have some bad implications for SEO since all of my pages regardless of if they exist or not are returning 200.

This definitely seems like a bug to me at the very least it is extremely unintuitive and should be documented. I would not expect streaming a response to completely change how SvelteKit reports the error on the server when requesting an html page. I would expect it when requesting data.json.

@eltigerchino
Copy link
Member

eltigerchino commented Nov 13, 2024

This is happening on SSR as well. If I hit this page from postman it will report back as 200 despite being 500. Currently all of my 404 pages are reporting back as 200. Look at the console.log from the server hook. nonBugged page reports the response correctly as 500, but the bugged one says 200 with the only difference between the 2 being a promise that returns nothing. This has been causing me a ton of problems with my telemetry as a lot of my errors cannot be found on the server and instead showing as ok responses. Also, I would think this would have some bad implications for SEO since all of my pages regardless of if they exist or not are returning 200.

I can't seem to reproduce this in the given reproduction with CSR disabled. Can you create another reproduction for this specific scenario?

@bertybot
Copy link
Contributor Author

Apologies for the bad repro I probably should have just made a repo to start as my computer has problems running web containers.

https://github.com/bertybot/kit-bug-error-code

I made a repro here. In more research it seems like I was wrong with it happening on pages with promises if csr is disabled. It seems to happen when the page has a layout that returns a streamed promise. The page is reported incorrectly even if you go there without CSR.

@eltigerchino eltigerchino added bug Something isn't working and removed awaiting submitter bug Something isn't working labels Nov 14, 2024
@eltigerchino
Copy link
Member

eltigerchino commented Nov 14, 2024

Seems like we can't return a 500 here because the layout data needs to be streamed for the error page. Hence, we need to send a 200 response status ahead of time before streaming the data. Is this correct? @dummdidumm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants