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

Add fetch parameter to entries() for seamless API integration #12980

Open
max-got opened this issue Nov 9, 2024 · 0 comments
Open

Add fetch parameter to entries() for seamless API integration #12980

max-got opened this issue Nov 9, 2024 · 0 comments
Labels
feature request New feature or request

Comments

@max-got
Copy link

max-got commented Nov 9, 2024

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

@max-got max-got changed the title Add fetch parameter to entries() for recursive prerendering from API routes Add fetch parameter to entries() for seamless API integration Nov 9, 2024
@eltigerchino eltigerchino added the feature request New feature or request label Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants