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

Make src/routes/index.anything.js a compile error #3751

Closed
bbaaxx opened this issue Feb 6, 2022 · 5 comments
Closed

Make src/routes/index.anything.js a compile error #3751

bbaaxx opened this issue Feb 6, 2022 · 5 comments
Labels
Milestone

Comments

@bbaaxx
Copy link

bbaaxx commented Feb 6, 2022

Describe the bug

An endpoint with a filename of routes/index.json.ts cannot be reached using a fetch from a routes/index.svelte

On the vite process I see a:

SyntaxError: Unexpected token < in JSON at position 0

The server defaults to 500 and the fetch promise fails.

Using any other name for the endpoint (like routes/other.json.ts) and pointing the fetch call to this new endpoint works as expected.

This was introduced on a recent version of @sveltejs/kit as this does not happen on previous versions this can be observed on : 1.0.0-next.260

Reproduction

  • run the gernerator: npm init svelte@next my-app
  • before installing deps use "@sveltejs/kit": "1.0.0-next.260"
  • create a routes/index.json.ts with a handler:
import type { RequestHandler } from "@sveltejs/kit";

export const get: RequestHandler = async () => {
  return {
    status: 200,
    body: {
      user: "Dude",
    },
  };
};
  • call the endpoint from routes/index.svelte
<script context="module" lang="ts">
  type Props = {
    props: {
      user: string;
    };
  };
  
  export const load = ({ fetch }): Promise<Props> =>
    fetch('/index.json') // does not work
    // fetch('/other.json') // works
      .then((res) => res.json())
      .then(({ user }) => ({ props: { user } }))
      .catch((res) => {
          console.error(res)
          return {
            status: res.status,
            error: new Error(`Could not load url`)
          };
      });
</script>

<...>
  • Rename routes/index.json.ts to something different like routes/other.json.ts and adjust the fetch call and it works as expected

Logs

<can't provide the logs>

System Info

System:
    OS: macOS 12.0.1
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
    Memory: 155.69 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.2/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.2/bin/npm
  Browsers:
    Edge: 97.0.1072.55
    Safari: 15.1
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.17
    @sveltejs/kit: 1.0.0-next.260 => 1.0.0-next.260
    svelte: ^3.44.0 => 3.46.4

Severity

serious, but I can work around it

Additional Information

Reported on the help #kit-help channel too

@benmccann
Copy link
Member

I haven't confirmed myself, but it was also mentioned on Discord that this is an example from the docs, so the docs are currently broken

@babichjacob
Copy link
Member

The only reference I see to index.json.js in the docs is inside a folder in routes rather than directly in routes.

What path is src/routes/index.json.js expected to be accessed from, with the knowledge that src/routes/index.svelte is accessed from website.com/? website.com.json and website.com/.json are both definitely wrong, but website.com/index.json differs from the meaning index has elsewhere.

@Conduitry
Copy link
Member

I don't know how index.something routes are treated at the root level. Generally, src/routes/foo/index.json would be a route at /foo.json. That could be part of this.

Also potentially adjacent is shadow endpoint stuff. If a GET endpoint has the same URL as a page, you can't request the endpoint unless you send Accept: application/json - although this generally shouldn't be an issue (unless you're trying to fetch the endpoint from a different page) because the response from that endpoint is automatically included as props in the page's load function without having to call it.

@Conduitry
Copy link
Member

Conduitry commented Feb 6, 2022

@babichjacob beat me to part of this by a few seconds. I'd probably argue that src/routes/index.anything.js or src/routes/index.anything.ts should probably be a compile-time error.

@benmccann benmccann changed the title routes/index.json.ts cannot be reached if fetched from routes/index.svelte Make src/routes/index.anything.js a compile error Feb 7, 2022
@benmccann benmccann added the feature request New feature or request label Feb 7, 2022
@Rich-Harris Rich-Harris added this to the 1.0 milestone May 11, 2022
@Rich-Harris
Copy link
Member

Closing as no longer necessary as of #5748

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

No branches or pull requests

5 participants