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

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

Closed
rsousacode opened this issue Feb 2, 2023 · 2 comments · Fixed by #8869
Closed

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

rsousacode opened this issue Feb 2, 2023 · 2 comments · Fixed by #8869
Labels
bug Something isn't working ready to implement please submit PRs for these issues!
Milestone

Comments

@rsousacode
Copy link

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

@rsousacode
Copy link
Author

The possible workaround is to use the full non-proxied path of the service when developing. After finishing the tests, change to relative path again and expect it to work in production (where the proxy does it's job like it does in calls in +page.server.js), I'll change the severity when I have this assumption tested.

@dummdidumm
Copy link
Member

Probably needs the same fix as #8453, setting state.initiator in endpoint.js#render_endpoint

@dummdidumm dummdidumm added bug Something isn't working ready to implement please submit PRs for these issues! labels Feb 2, 2023
@dummdidumm dummdidumm added this to the soon milestone Feb 2, 2023
@rsousacode rsousacode changed the title +server.js - fetch call: Error: Not found: (but works on +page.server.js) +server.js - fetch call: Error: Not found (but works on +page.server.js) Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ready to implement please submit PRs for these issues!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants