question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding non-serialisable metadata to toState

See original GitHub issue

This is a follow up of https://github.com/router5/router5/issues/60#issuecomment-233296870

@troch I’m hitting the issue that @iotch mentioned… I would like to attach non-serializable stuff into toState:

// getComponentMiddleware.js
export default (routes) => (router) => (toState, fromState) => {
    const { toActivate } = transitionPath(toState, fromState);

    // Get the routes involved in the transition
    const routesToActivate = toActivate.map((segment) => routes.find((route) => route.name === segment));

    // Build a map in which keys are the route names and values that resolve to their components
    const promisesMap = fromPairs(routesToActivate.map((route) => [route.name, route.component || route.getComponent()]));

    return pProps(promisesMap)
    .then((components) => ({ ...toState, components }));
};

DOMException: Failed to execute 'pushState' on 'History': An object could not be cloned.

So here are a few questions:

  • Do we really have to persist the state to the history api? browser.getState is only used here. Isn’t it more correct to compare the URLs instead of comparing states? In my opinion, the browser plugin is just a way to set and react to URL changes, it shouldn’t be storing nor handling states. Also, this would remove the inconsistency that exists now between using history API or hashbangs.
  • If you would like to keep it like this, could we have field that would not be included in the history state? This would allow us to add big data structures (big structures generate QUOTA errors) or unserializable data to be added to toState.

Thanks in advance.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
trochcommented, Jun 13, 2017

@satazor this has been fixed with v4.7.1: only base state properties (meta, name, path and params) are pushed to history.

1reaction
motet-acommented, May 11, 2017

Yes. I think the best is to allow the user to customize entirely the way the state is stored with simple options. I think something in the spirit of react-redux could be suitable:

let savedBigData

function mapRouterStateToBrowserState({ bigData, ...state }) {
    savedBigData = bigData
    return state // Will be saved in window.history.state (bigData has been extracted)
}

function mapBrowserStateToRouterState(browserState) {
    return {
        ...browserState, 
        bigData: savedBigData,
    }
}

router.usePlugin(
    browserPlugin({
        mapRouterStateToBrowserState,
        mapBrowserStateToRouterState,
    })
)

mapRouterStateToBrowserState and mapBrowserStateToRouterState can both default to an identity function, s => s. This is not breaking changes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java: What can and what can't be serialized? - Stack Overflow
First of all, if you don't plan to ever serialize an instance of your class, there is no need to even think about...
Read more >
The redux best practice "Do Not Put Non-Serializable Values ...
This article explains one of the four react-redux best practices: "Do Not Put Non-Serializable Values in State or Actions"
Read more >
NonSerializedAttribute Class (System) - Microsoft Learn
Indicates that a field of a serializable class should not be serialized. This class cannot be inherited.
Read more >
R Packages (2e) - 10 DESCRIPTION
The DESCRIPTION file provides overall metadata about the package, such as the package name and which other packages it depends on. The NAMESPACE...
Read more >
Bug? Why doesn't JSON.serialize support ...
As of API v42.0 this is still not fixed. While serializing in the Dev Console works fine calling the code from a trigger...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found