WICG / portals

A proposal for enabling seamless navigations between sites or pages

Home Page:https://wicg.github.io/portals/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can portals be used for SPA routing?

markcellus opened this issue · comments

commented

Hello!

I was directed here by another thread and portals look great! It seems like many use-cases may be covered, but I couldn't find any detail that helps verify if portals can be used for single page application routing.

Many times developers want to just update DOM when a user has accessed some relatively deeper URL in the browser.

  1. User goes to mysite.com
  2. index HTML loads
  3. User clicks on a link to take them to mysite.com/about-us
  4. Application replaces parts of index HTML in DOM with "About Us" html

Can this be done with portals without replacing all contents of the HTML? I'd like to just replace parts of HTML when navigating to new inset page.

I don't think I see how portals could be used for your use case: it provides a mechanism for smoothly transitioning between frames holding entirely separate documents.

You could achieve what you describe either by using portals and arranging to transfer the state in the rest of your HTML, but more likely you would use a traditional SPA approach (fetching the parts of the HTML that you want from the server with XHR/fetch and updating the DOM yourself).

It's not clear to me how the UA could tell what parts of the DOM you would want replaced, or how much prerendering we could reasonably do if you intend to transplant everything into a separate document.

commented

Yeah, I think what I'm looking for is route mapping: the ability to assign a portal or inset to a requested URL in an SPA. This is what could possibly determine what parts of the DOM would be replaced. Similar to what third-party libraries like react-router and vue-router do.

It's not obvious to me what portals would add to those use case over the existing libraries in the space, especially if the use case involves inserting and rendering new (or at least newly adopted) DOM.

commented

Oh yeah, what I'm proposing is definitely solved in existing libraries. In fact, I created one of them 😉 . I guess I was just hoping that portals would eliminate having to rely on existing libraries for the use case. But I think my initial question is answered now so will close this issue. Thanks for the information!