solidjs / solid-router

A universal router for Solid inspired by Ember and React Router
MIT License
1.1k stars 137 forks source link

Absolute redirects inside `cache` are ignored if not called in `load` #438

Closed Brendonovich closed 1 month ago

Brendonovich commented 1 month ago

Describe the bug

From https://github.com/solidjs/solid-start/issues/1512#issuecomment-2149045594

import { type RouteDefinition } from '@solidjs/router'
import { cache, createAsync, redirect } from '@solidjs/router'
import { getRequestEvent } from 'solid-js/web'

const exampleAction = cache(async () => {
  'use server'
  const location = new URL(getRequestEvent()!.request.url)
  return redirect(`${location.origin}/target`)
}, 'exampleAction')

export const route = {
  load() {
    // redirect will start working if uncommented:
    // void exampleAction()
  },
} satisfies RouteDefinition

export default function Component() {
  const data = createAsync(() => exampleAction())
  return <h1>Result: {data()}</h1>
}

With the route above, when you navigate to /redirect, it does not redirect you to /target.

Unless you trigger an action with redirect inside route.load or use a relative redirect which starts with / instead of a full URL with the origin, i.e., redirect('/target') works, but redirect('http://localhost:3000/target') does not.

Your Example Website or App

N/A

Steps to Reproduce the Bug or Issue

Use the above code in an SSR route

Expected behavior

Redirect happens without a load function.

Screenshots or Videos

No response

Platform

N/A

Additional context

Am fixing this rn