18

I normally use the following to redirect to any page without using window.location.href in Sveltekit

import { goto } from '$app/navigation';

const goSomeWhere = () :void => {
    goto('/')
}

But how do we make it go back to the previous page? Should I just use the vanilla JavaScript to go back?

0

5 Answers 5

17

You can go back to the previous page by using the afterNavigate lifecycle.

This can be more reliable than @Jarduilno's suggestion as not every incoming link is in the same parent path.

store the pathname from the from URL object, and use it later in a function. ex: goto(previousPage)

import { goto, afterNavigate } from '$app/navigation';
import { base } from '$app/paths'

let previousPage : string = base ;

afterNavigate(({from}) => {
   previousPage = from?.url.pathname || previousPage
}) 
Sign up to request clarification or add additional context in comments.

1 Comment

once you go back to another page this way, page you came from will be "previous", so if you click button again you will go the the page you came from, thus making a loop from these two pages
15

You can use the javaScript history object like this history.back();

3 Comments

It won't trigger recomposition.
Works for me. And since SvelteKit should correctly handle click on the Back button in the web browser, it should also be able to correctly handle back navigation this way (I don't think there exists a way for SvelteKit to distinguish the two ways).
This is the correct answer... svelte.dev/docs/kit/shallow-routing
4

in our SvelteKit project we are sometimes navigating to previous page like in the example bellow

import { goto } from '$app/navigation';
import { page } from '$app/stores';

const goSomeWhereBack = () => {
    goto($page.url.pathname.substring(0, $page.url.pathname.lastIndexOf('/')));
}

Comments

3

You can made use of the navigating store from $app/stores.

import { navigating } from "$app/stores";

export const previousPage = readable(null, (set) => {
    const unsubscribe = navigating.subscribe(($navigating) => {
        // Check if `$navigating` has a value
        // because it's set to `null` after navigation is done
        if ($navigating) {
            set($navigating.from.url.pathname);
        }
    });

    return () => unsubscribe();
});

Keep in mind that this store is keeping track of the previously opened page and is not a stack. So calling goto($previousPage) multiple times one after the other will get you stuck in a loop.

Alternatively, if you want to use the History API instead (which uses a stack), you can just call history.back() as mentioned in this answer.

Comments

1

I found a hacky solution, you can use window.history.back() and then after some time call goto(window.location.href). This triggers re-render(?) which I needed because otherwise simple window.history.back() didn't work.

window.history.back();
setTimeout(() => goto(window.location.href), 100)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.