hotwired / turbo-android

Android framework for making Turbo native apps
MIT License
407 stars 51 forks source link

Do not leave the app if navigating in a subdomain #330

Closed arlee17 closed 2 months ago

arlee17 commented 2 months ago

In my application I have several subdomains for example:

it-it.mydomain.com en-us.mydomain.com es-es.mydomain.com

The BASE_URL is mydomain.com

When I try to navigate to these subdomains the emulator browser opens.

Being the same domain shouldn't it stay inside the app? If not, is there a way to not exit the app and visit the subdomains?

Here is my code in the main WebFragment:

@TurboNavGraphDestination(uri = "${SHOP_NAME}://fragment/main")
open class WebFragment : TurboWebFragment(), TurboNavDestination {

  override fun shouldNavigateTo(newLocation: String): Boolean {
    return when (isNavigable(newLocation)) {
      true -> true
      else -> {
        launchBrowser(newLocation)
        false
      }
    }
  }

  private fun isNavigable(location: String): Boolean {
    return location.startsWith(BASE_URL)
  }

  private fun launchBrowser(location: String) {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(location))
    context?.startActivity(intent)
  }

  override fun getNavigationOptions(
    newLocation: String,
    newPathProperties: TurboPathConfigurationProperties
  ): NavOptions {
    return when (newPathProperties.context) {
      MODAL -> slideAnimation()
      else -> {
        super<TurboWebFragment>.getNavigationOptions(newLocation, newPathProperties)
      }
    }
  }

  private fun slideAnimation(): NavOptions {
    return navOptions {
      anim {
        enter = R.anim.custom_anim_enter
        exit = R.anim.custom_anim_exit
        popEnter = R.anim.custom_anim_pop_enter
        popExit = R.anim.custom_anim_pop_exit
      }
    }
  }
}
felipejoglar commented 2 months ago

Have you tried to modify the logic inside the isNavigable(location: String)? It is only checking that the newLocation starts with the BASE_URL. Maybe you can try with a contains, or a more sophisticated regex approach.

arlee17 commented 2 months ago

Thank you Felipe for your answer.

Actually I tried with contains and I get this error:

[INFO:CONSOLE(21)] "Uncaught (in promise) SecurityError: Failed to execute 'pushState' on 'History': A history state object with URL 'https://es-es.mydomain.com/' cannot be created in a document with origin 'https://it-it.mydomain.com' and URL 'https://it-it.mydomain.com/'.", source: https://cdn.jsdelivr.net/npm/@hotwired/turbo@7.3.0/dist/turbo.es2017-esm.min.js 

Any other idea? Thanks again!

felipejoglar commented 2 months ago

Hi @arlee17,

Did you get another log right below the one you showed? That states something like:

[INFO:CONSOLE(0)] "Access to fetch at 'https://es-es.mydomain.com/' from origin 'https://it-it.mydomain.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.", source: https://it-it.mydomain.com/ (0)

I miss some backend background here but tweaking the Access-Control-Allow-Origin may help in this case.

arlee17 commented 2 months ago

@felipejoglar Thanks again for your answer!

I managed not to exit the app using contains.