Render a new router and redirect in action of opened component doesn't redirect in the opened window

Example codes
export function winOpen(provider: string) {
if (window) {
const win = window.open(
"",
"",
);
if (win) {
render(
() => (
<Router
root={() => {
return <SomeComponent />;
}}
/>
),
win.document.body,
);
}
}
}
function SomeComponent(){
const action = useAction(someaction);
}
const someaction = action(async(){
return redirect("https://www.google.com")
})
export function winOpen(provider: string) {
if (window) {
const win = window.open(
"",
"",
);
if (win) {
render(
() => (
<Router
root={() => {
return <SomeComponent />;
}}
/>
),
win.document.body,
);
}
}
}
function SomeComponent(){
const action = useAction(someaction);
}
const someaction = action(async(){
return redirect("https://www.google.com")
})
why the redirect took places in the original window tab not in the opened window?
5 Replies
bigmistqke
bigmistqke2mo ago
because everything is still being done in the original window, only the resulting html is being rendered in the new window.
colinshen
colinshen2mo ago
Is this the browser default behavior? I thought I render a new router in side the window and a new router context was created.
peerreynders
peerreynders2mo ago
<SomeComponent> lives under the existing router in the tree. So redirect() in the action used inside of <SomeComponent> is serviced by that router. redirect() is imported from @solidjs/router which tells you it's not a browser function.
peerreynders
peerreynders2mo ago
Also note from the documentation that redirects are thrown, not returned. That way the router can catch the surrogate Response and process it (the practice of throwing and catching non-Errors is called a nonlocal return).
Is this the browser default behavior?
In short yes. When you click a link in a document (i.e. navigate), the content of the response from href replaces the current content of the window. target="_blank" is necessary to force the link to open in a new window.
MDN Web Docs
: The Anchor element - HTML: HyperText Markup Language | MDN
The HTML element (or anchor element), with its href attribute, creates a hyperlink to web pages, files, email addresses, locations in the same page, or anything else a URL can address.
colinshen
colinshen2mo ago
I made a mistake in the above example. SomeComponent calls createAsync instead of createAction(its not a post action). I found the solution, I should not use the redirect in the server function ,just return the url and use location.replace with the new url. Thanks!!