Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document Title doesn't change with m.route.set options #2600

Closed
agentx-cgn opened this issue May 29, 2020 · 3 comments
Closed

Document Title doesn't change with m.route.set options #2600

agentx-cgn opened this issue May 29, 2020 · 3 comments
Assignees
Labels
Type: Gotcha For unwanted behavior that is still correct under the given circumstances

Comments

@agentx-cgn
Copy link

Browsers seem to ignore the pushState/replaceState option title. As consequence pages in the browser's history are indistinguishable. The workaround is to set the title with document.title = 'Your title here'. Mithril should handle this gracefully and automatically.

Discussion: whatwg/html#2174
Solution: https://stackoverflow.com/questions/13955520/page-title-is-not-changed-by-history-pushstate
Version: as of today

@agentx-cgn agentx-cgn added the Type: Bug For bugs and any other unexpected breakage label May 29, 2020
@dead-claudia
Copy link
Member

Known issue with no intention to fix. This is up to the browsers to implement, not us. It's worth noting that not all browsers ignore it this blatantly.

If anything, I'm tempted to drop support for the title parameter entirely, but that isn't going to happen before v3.

@dead-claudia dead-claudia added Type: Gotcha For unwanted behavior that is still correct under the given circumstances and removed Type: Bug For bugs and any other unexpected breakage labels May 30, 2020
@agentx-cgn
Copy link
Author

Well, sure, the specs say: The title is purely advisory. User agents might use the title in the user interface. But, at the moment it is up to the users to implement, which requires a particular architecture. At least an example might be advisable.

@dead-claudia
Copy link
Member

@agentx-cgn If you really want a true drop-in workaround, just write a helper like this:

function setRoute(path, params, opts) {
    var prev = document.title
    try {
        if (opts && opts.title) document.title = opts.title
        m.route.set(path, params, opts)
    } finally {
        if (opts && opts.title) document.title = prev
    }
}

That should likely be sufficient for your needs, as browsers I believe should honor that. If that doesn't work (as in document.title isn't captured synchronously - not 100% sure when browsers do capture it), you can do something like this:

function setRoute(path, params, opts) {
    if (opts && opts.title) {
        var prev = document.title
        // This listener is intentionally capturing, so it precedes
        // Mithril's own listener
        window.addEventListener("popstate", function handler() {
            window.removeEventListener("popstate", handler, true)
            document.title = prev
        }, true)
        document.title = opts.title
        try {
            m.route.set(path, params, opts)
        } catch (e) {
            handler()
            throw e
        }
    } else {
        m.route.set(path, params, opts)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Gotcha For unwanted behavior that is still correct under the given circumstances
Projects
Status: Closed
Development

No branches or pull requests

2 participants