Ask Your Question
3

How can I add the base URL to 404.html using Vite?

asked 2023-05-23 10:09:29 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-23 10:18:01 +0000

bukephalos gravatar image

In Vite, you can add the base URL to the 404.html file by including the following code at the top of the file:

<script>
  const baseUrl = import.meta.env.BASE_URL;
  const url = new URL(window.location.href);
  url.pathname = baseUrl + '/404.html';
  window.location = url.href;
</script>

This code gets the current base URL from the Vite environment variable import.meta.env.BASE_URL, then modifies the current URL and redirects the user to the 404 page with the updated base URL.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-23 10:09:29 +0000

Seen: 8 times

Last updated: May 23 '23