小程序/APP内嵌H5,第一次加载完成后点location.href打开新的连接后返回页面404的问题记录

3 阅读1分钟

返回404,打印window.history发现里面没有东西。说明浏览器页面history栈里没有刚刚打开的H5。 执行在页面加载完成的时候把页面栈中加入稳定的URL

  const cleanUrl = window.location.origin + window.location.pathname;
  //  window.history.replaceState 替换历史记录为稳定URL
  window.history.replaceState({}, document.title, cleanUrl);