Skip to content

+server.js - fetch call: Error: Not found (but works on +page.server.js) #8851

@rsousacode

Description

@rsousacode

Describe the bug

I am trying to implement a RSS feed similarly to what the docs explain for doing a sitemap (https://kit.svelte.dev/docs/seo#manual-setup-sitemaps).

In the +server.js, When trying to do a fetch call to the endpoint "/api/posts", I get a 404 response when using "event.fetch" and then a crash:

function getFeedItems(fetch) {  
     return fetchPosts(fetch).then(data => {
     console.log(data); // JSON parse error, because i received a 404 html response
     }
  );
}

export async function GET(event) {  
    getFeedItems(event.fetch);  
    return new Response( `...` 
         .trim(),    
           {      
               headers: {        
                    'Content-Type': 'application/xml'      
                }    
           }  
     );
}

I've tested calling this endpoint from a +page.server.js, and the data is retrieved as expected. But not using event.fetch in a +server.js (also tested using fetch in place of event in the exported GET)

Reproduction

  1. git clone https://github.com/rsousacode/AspNetSPA_SvelteKit && cd AspNetSPA_SvelteKit && git checkout maybe-bug
  2. Run dotnet run (it'll run the whole thing)
  3. In the menu, click on "Not working" and check routes/not-working to see the code

Reproduction github repo: https://github.com/rsousacode/AspNetSPA_SvelteKit/tree/maybe-bug

Logs

Error: Not found: /api/posts
    at resolve (/node_modules/@sveltejs/kit/src/runtime/server/respond.js:391:13)
    at resolve (/node_modules/@sveltejs/kit/src/runtime/server/respond.js:232:5)
    at #options.hooks.handle (/node_modules/@sveltejs/kit/src/runtime/server/index.js:42:55)
    at Module.respond (/node_modules/@sveltejs/kit/src/runtime/server/respond.js:229:40)
    at async fetch (/node_modules/@sveltejs/kit/src/runtime/server/fetch.js:134:16)
    at async eval (/node_modules/@sveltejs/kit/src/runtime/server/fetch.js:27:10)
undefined:1
<!DOCTYPE html>
^

SyntaxError: Unexpected token < in JSON at position 0

System Info

MacOs Arm64
Node v18.13.0
Kit: 1.1.0 (main project)

Severity

blocking an upgrade

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingready to implementplease submit PRs for these issues!

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions