Skip to content

Instantly share code, notes, and snippets.

@jokeyrhyme
Last active November 11, 2016 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jokeyrhyme/241f20f3a4029def61e25ca4eb513311 to your computer and use it in GitHub Desktop.
Save jokeyrhyme/241f20f3a4029def61e25ca4eb513311 to your computer and use it in GitHub Desktop.
detect-ionic-deploy.js
// accurate _after_ Cordova's "deviceready" event
function isIonicDeployPath (
appId /* : string */,
global /* : Window | Global */
) /* : Promise<boolean> */ {
'use strict'
if (!global.IonicDeploy || !global.IonicDeploy.info) {
return Promise.resolve(false)
}
let pathname = ''
try {
pathname = (new URL(global.location.href)).pathname
} catch (err) {} // swallow errors, use initial empty string
return new Promise((resolve, reject) => {
global.IonicDeploy.info(appId, resolve, reject)
})
.then((data) => data.deploy_uuid || '') // TODO: cache deploy_uuid to avoid future Cordova plugin calls
.then((deploy_uuid) => {
const ending = deploy_uuid + '/index.html'
return pathname.slice(pathname.length - ending.length) === ending
})
.catch(() => false) // swallow errors, just return `false`
}

How to detect Ionic Deploy and related URLs

Research Snippets

IonicDeploy.info(appId, function (data) { /* ... */ }, function (err) { /* ... */ })
(new URL(location.href)).pathname

Observations

Android

Tested on Nexus 9 running Android 7.0

fresh install, no updates via deploy

  • deploy_uuid: "cca9d318978b0e8959d83876c8a10ead"

  • pathname: "/data/user/0/io.cordova.hellocordova/app_cca9d318978b0e8959d83876c8a10ead/index.html"

after an update via deploy

  • deploy_uuid: "cdc35956368f2c97c868f64c759e56de"

  • pathname: "/data/user/0/io.cordova.hellocordova/app_cdc35956368f2c97c868f64c759e56de/index.html"

iOS

Tested in iOS Simulator for 10.1

fresh install, no updates via deploy

  • deploy_uuid: "6f551a0e86ebc2cdf822405e5f17e9fb"

  • pathname: "/REDACTED_SIMULATOR_PATH/data/Containers/Data/Application/REDACTED_APP_UUID/Library/Application%20Support/6f551a0e86ebc2cdf822405e5f17e9fb/index.html"

after an update via deploy

  • deploy_uuid: "5af1a24a4f97e87e399c8489bb22ab3a"

  • pathname: "/REDACTED_SIMULATOR_PATH/data/Containers/Data/Application/REDACTED_APP_UUID/Library/Application%20Support/5af1a24a4f97e87e399c8489bb22ab3a/index.html"

Inferences

  • "deploy_uuid" string value is found precisely as-is within "pathname" string

  • "pathname" string value ends with "DEPLOY_UUID/index.html"

See Also

@jokeyrhyme
Copy link
Author

Pinging Ionic developers @Fuiste and @ericb, just an FYI :)

@jokeyrhyme
Copy link
Author

I'm looking into this in the first place because webview apps load from "file:" and seem to have problems with the out-of-the-box configuration for HTML5-based client-side routers:

From these suggestions it looks like hash-based routing is much more likely to function as expected out of the box.

There are other benefits to hash-based routing: http://jamesknelson.com/push-state-vs-hash-based-routing-with-react-js/

@jokeyrhyme
Copy link
Author

For react-router-starter-kit 3.0.0-alpha.2, the following tweaks are all that is required (no path detection funny business):

  • in config/environment.js: change production compiler_public_path from '/' to '' (empty string)

  • in src/main.js: use 'history/lib/createHashHistory' instead of 'history/lib/createBrowserHistory'

@jokeyrhyme
Copy link
Author

jokeyrhyme commented Nov 10, 2016

For jQueryMobile 1.3.x, set pushStateEnabled to false (at least after detecting "file:" protocol or similar)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment