PWABuilder icon indicating copy to clipboard operation
PWABuilder copied to clipboard

[Docs][iOS] Deep links/shortcuts/scheme links and SPAs

Open justrealmilk opened this issue 3 years ago • 6 comments
trafficstars

This is a docs suggestion. I've been investigating these features for the past day and in addition to https://github.com/pwa-builder/pwabuilder-ios/pull/47 I'd like to make the following suggestion.

SPAs will reload when window.location.href is set to a different URL because of the way router libraries work. For react-router-dom users (and probably very similar for users of other environments), you can use something similar to the following.

In place of

PWAShell.webView.evaluateJavaScript("location.href = '\(universalLink)'")

you can use

PWAShell.webView.evaluateJavaScript("window.routerPush !== undefined ? 
  // adjust URL to match router requirements - react-router-dom only wants the path
  window.routerPush('\(universalLink)'.replace('https://mypwa.domain.com','')) 
  : window.location.href = '\(universalLink)'")
  // line breaks added for readability

In your SPA, you place a small component within your router component tree

<BrowserRouter>
  <ExposeHistory />
</BrowserRouter>

where ExposeHistory is

function ExposeHistory() {
  // react-router-dom v5 - I think you just change the hook to useNavigate with v6
  const history = useHistory();
  
  useEffect(() => {
    if (history !== undefined && window.routerPush === undefined) {
      window.routerPush = history.push;
    }
  }, [history]);
  
  return null;
}

This makes the router libraries history abstraction available in the window scope where our Swift apps can easily call a function to push a new path!

justrealmilk avatar Feb 01 '22 21:02 justrealmilk

Hello justrealmilk, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will investigate the issue and help solve it ASAP. Other community members may also look into the issue and provide feedback 🙌

fallenleavesguy avatar Feb 01 '22 21:02 fallenleavesguy

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

ghost avatar Feb 17 '22 02:02 ghost

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

paulmolluzzo avatar Mar 04 '22 05:03 paulmolluzzo

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

ghost avatar Mar 19 '22 05:03 ghost

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

ghost avatar Apr 03 '22 08:04 ghost

cc @zateutsch for visibility

nmetulev avatar Apr 12 '22 17:04 nmetulev