2 Questions

How, that’s simple.

For that we have the session storage API which allows storing data on a user session.

So the steps are

First, on each page, store the current page in session storage. Best to do so in site code (see the tabs to the left of the coding area)?

The code will look something like (this is on site code)

import {session} from 'wix-storage';
import location from 'wix-location';
$w.onReady(() => {
  let history = JSON.parse(session.getItem('history') || '[]');
  history.push(location.url);
  session.setItem('history, JSON.stringify(history));    
});

Next, on any page with the back button, we hide it if there is no history (this is on the page code)

import {session} from 'wix-storage'; 
import location from 'wix-location'; 

let history = JSON.parse(session.getItem('history'));
$w.onReady(() => {
  if (!history || history.length === 0)
    $w('#backButton').hide();
}); 

Next, on the button on click event, you read the history and navigate to it

export function backButton_onClick(event) {
  if (history && history.length > 0) {
    let lastPage = history.pop();
    session.setItem('history, JSON.stringify(history)); 
    location.to(lastPage);
  }
}