Skip to content

Determine whether a view template is being rendered by the inertia custom renderer #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Andy9822 opened this issue Apr 7, 2025 · 4 comments · Fixed by #209
Closed

Comments

@Andy9822
Copy link
Contributor

Andy9822 commented Apr 7, 2025

I noticed that Inertia sets an instance variable @_inertia_ssr_head during SSR rendering:

controller.instance_variable_get("@_inertia_ssr_head")

We're planning to gradually migrate our Rails application to use Inertia, and I need a way to detect in our application.html.erb layout whether the current request is being rendered through Inertia. This would help us conditionally include the appropriate JavaScript packs.

Currently, there doesn't seem to be a way to detect if we're in an Inertia rendering context from the application layout (I may be wrong here, but so far I didn't find anything I could use).
Would it be acceptable if I submit a PR that adds an instance variable (e.g., @_inertia_rendering) in the Renderer class?

module InertiaRails
class Renderer

This would allow us to conditionally include assets in our layout like:

<% if @_inertia_rendering %>
  <%= javascript_include_tag "inertia" %>
<% end %>
@Andy9822 Andy9822 changed the title Find whether a view template is being rendered by the inertia custom renderer Determine whether a view template is being rendered by the inertia custom renderer Apr 7, 2025
@skryukov
Copy link
Contributor

skryukov commented Apr 7, 2025

Hey @Andy9822! Sounds like a great addition, I'd also add a view helper i.e.

def inertia_rendering?
  controller.instance_variable_get("@_inertia_rendering")
end

@Andy9822
Copy link
Contributor Author

Andy9822 commented Apr 7, 2025

Hey @Andy9822! Sounds like a great addition, I'd also add a view helper i.e.

def inertia_rendering?
controller.instance_variable_get("@_inertia_rendering")
end

cool cool cool! will give it a try later today

also, great presentation about Inertia the other day, congratz! I enjoyed a lot talking with you afterwards 😄

@Andy9822
Copy link
Contributor Author

Andy9822 commented Apr 9, 2025

Hey @Andy9822! Sounds like a great addition, I'd also add a view helper i.e.
def inertia_rendering?
controller.instance_variable_get("@_inertia_rendering")
end

cool cool cool! will give it a try later today

also, great presentation about Inertia the other day, congratz! I enjoyed a lot talking with you afterwards 😄

I'm not so familiar with open source and contributing to projects, so sorry for silly questions or things, but I could find a way to assign anyone as reviewer to the PR I opened addressing this here: #209

how should I formally proceed in these situations?
cc @skryukov

@bknoles
Copy link
Collaborator

bknoles commented Apr 10, 2025

One other option you might consider for the use case that prompted this feature: Inertia respects the Rails layout configured by the controller, so you could create a separate layout for Inertia based controllers, even per action if you are migrating one route at a time. Here's an example of that from the test suite.

(I say might consider because the specifics of your application.html.erb could certainly mean it's cleaner to conditionally add the inertia javascript via inertia_rendering?)

Either way, this is a nice addition to the library! Thanks for the contribution @Andy9822 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants