Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt): router with hash mode #6980

Merged
merged 4 commits into from
Sep 4, 2022
Merged

feat(nuxt): router with hash mode #6980

merged 4 commits into from
Sep 4, 2022

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Aug 26, 2022

πŸ”— Linked issue

nuxt/nuxt#13821

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This PR adds support for hash history mode for router (client only) with a new option. In order to enable:

import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
  ssr: false,
  router: {
    options: {
      hashMode: true
    }
  }
})

Remarks:

  • This option only works for spa mode
  • Implementation of createWebHashHistory is super simple. It basically appends # to the base with location.host and reuse createWebHistory. So there is almost no bundle overhead for this option
  • I know, API is not beautiful but this is also an edge option! Later, we might move router.options to app.router this way option would be simpler app.router.hashMode: false instead of router.options.hashMode: false. router.mode from Nuxt 2 is also deprecated. We could portal it but it makes it harder to merge with one options object.

TODO:

  • Add example to the docs
  • Investigate issue with initial route pushing additional /# each time
  • Somehow warn about not using ssr: false with hashMode: true

@netlify
Copy link

netlify bot commented Aug 26, 2022

βœ… Deploy Preview for nuxt3-docs ready!

Name Link
πŸ”¨ Latest commit 85ef2f7
πŸ” Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/63145d4fe8d091000810c5b0
😎 Deploy Preview https://deploy-preview-6980--nuxt3-docs.netlify.app
πŸ“± Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@pranavxc
Copy link

Thanks for the PR, we have been struggling due to the issue since last week. As a workaround we have updated the build files under node_modules directly ( node_modules/nuxt/dist/pages/runtime/router.mjs ).

For enabling hash-based routing we had modified

import {
  createRouter,
  createWebHistory,
  createMemoryHistory
} from "vue-router";

to

import {
  createRouter,
  createWebHashHistory as createWebHistory,
  createMemoryHistory
} from "vue-router";

And fixed following issue mentioned in PR

Investigate issue with initial route pushing additional /# each time

by changing following

function createCurrentLocation(base, location) {
  const { pathname, search, hash } = location;

to

function createCurrentLocation(base, location) {
  const { pathname, search, hash } = new URL((location.hash || '').replace(/^#/, ''), location.origin);

As a temporary workaround it's working πŸ‘ waiting for your PR to merge.

@bcakmakoglu
Copy link

@pi0 is there something we could potentially help with to get this PR merged quicker? πŸ˜„

@pi0
Copy link
Member Author

pi0 commented Aug 29, 2022

WoW I didn't expect this amount of welcoming for this particular feature <3 Surely we can move it to the edge. Only there is an issue that redirects to #/#/ everytime page is refreshed. I'm worried we are missing something. When fixed we can move to the edge.

@pi0 pi0 marked this pull request as ready for review September 4, 2022 08:09
@pi0 pi0 merged commit ac56ce7 into main Sep 4, 2022
@pi0 pi0 deleted the feat/router-history branch September 4, 2022 08:11
@pi0 pi0 mentioned this pull request Sep 9, 2022
@pi0 pi0 mentioned this pull request Sep 15, 2022
@danielroe danielroe added the 3.x label Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants