sveltejs / sveltejs/kit

Add `fetch` parameter to `entries()` for seamless API integration

Open
#12,980 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

The EntryGenerator's entries() function would benefit from having access to SvelteKit's enhanced fetch, enabling direct integration with API routes during the prerendering process.

Current Limitation:
Currently, when using the EntryGenerator pattern with dynamic routes that depend on API data, we need to duplicate the data fetching logic or import it directly from the filesystem. This is both less elegant and diverges from how we normally access data in SvelteKit applications.

Use Case:
Consider this setup:

// src/routes/api/items/+server.js
export async function GET() {
  // Returns prerendered API data
  return json(items);
}

// src/routes/items/[id]/+page.server.js
export const prerender = true;

/** @type {import('./$types').EntryGenerator} */
export async function entries({ fetch }) {  // <- Add fetch parameter
  // Can safely fetch from API routes
  const res = await fetch('/api/items');
  const items = await res.json();
  
  return items.map(item => ({
    id: item.id
  }));
}

export async function load({ fetch, params }) {
  const res = await fetch(`/api/items/${params.id}`);
  const item = await res.json();
  return { item };
}

Benefits:

  • Consistent data fetching pattern across load and entries functions
  • Reuse existing API routes for entry generation
  • Keep data fetching logic DRY
  • Works seamlessly with SvelteKit's prerendering system
Describe the proposed solution
/** @type {import('./$types').EntryGenerator} */
export async function entries({ fetch }) {
  // Now we can use the same fetch as in load functions
  const res = await fetch('/api/items');
  const items = await res.json();
  return items.map(item => ({ id: item.id }));
}
Alternatives considered
  • Manual data imports: Less maintainable, duplicates logic
  • Direct filesystem access: Breaks the API abstraction, less portable
  • Custom fetch implementation: Unnecessary given SvelteKit's existing fetch capabilities
Importance

would make my life easier

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the EntryGenerator entries() entry point and the prerendering path that supplies its arguments. Check how enhanced fetch is made available to load functions, then identify the relevant type and behavior tests. Done means entries({ fetch }) can fetch the /api/items route during prerendering without duplicating data access logic.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.