Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cannot redirect() from within API Route #3614

Open
uap-dev opened this issue Mar 1, 2025 · 1 comment
Open

Cannot redirect() from within API Route #3614

uap-dev opened this issue Mar 1, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@uap-dev
Copy link

uap-dev commented Mar 1, 2025

Which project does this relate to?

Start

Describe the bug

Might not be a bug, but seems like something that we should be able to do to take advantage of type safety in routing. Cannot use redirect method from within APIRoute. Can use the raw response object way of doing things, but leveraging the redirect primitive for type safety would be nice.

import { createAPIFileRoute } from '@tanstack/react-start/api';
import { deleteCookie } from '@tanstack/react-start/server';
import { redirect } from '@tanstack/react-router';

export const APIRoute = createAPIFileRoute('/api/logout')({
  GET: ({ request }) => {
    console.log('/api/logout');
    deleteCookie('my_session_cookie');


    // Uncomment below (Works)
    // const response = new Response(null, { status: 302 });
    // response.headers.set('Location', '/#');
    // return response;

    // Doesn't work
    throw redirect({
      to: '/#',
    });
  },
});

Your Example Website or App

https://github.com/uap-dev/before_load_error/tree/api_route_no_redirect

Steps to Reproduce the Bug or Issue

  1. npm run dev
  2. nav to /api/logout
  3. expect redirect to /#, leveraging redirect primitive from @tanstack/react-router

Expected behavior

Would expect to be able to leverage redirect for type safety inside API routes.

Screenshots or Videos

Image

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

@schiller-manuel schiller-manuel added the enhancement New feature or request label Mar 1, 2025
@schiller-manuel
Copy link
Contributor

not a bug, just not supported. maybe never will

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants