inertiajs / inertia-rails

The Rails adapter for Inertia.js.

Home Page:https://inertiajs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deep merge shared data

bknoles opened this issue · comments

One use case for shared data is to provide defaults that individual controllers and actions can override. For example, meta tag data:

class ApplicationController
  # Let's assume the frontend knows how to digest this and print it to the page
  inertia_share do
    {
      meta: {
        title: 'Who is the GOAT?',
        description: 'A place for Brandon and Brian to debate Jordan vs. Brady',
      }
    }
  end
end

class HockeyController < ApplicationController
  def index
    render inertia: 'HockeyPage', props: {
      meta: {
        title: 'Who is the hockey GOAT?',
        description: 'Probably Gretzky, right?',
      }
    }
  end
end

No issues so far! But it's a pain when you only want to overwrite some of the props, like:

class BasketballController < ApplicationController
  def index
    render inertia: 'BasketballPage', props: {
      meta: {
        title: 'Who is the basketball GOAT?',
        # I want the default description
      }
    }
  end
end

Solving this is not difficult; we just need to deep merge props fed to the renderer into the shared data.

The tricky parts are whether this should be opt-in behavior and whether or not it should be default behavior.

As I typed that out, I decided I don't want this to be a breaking change and that this should be optional/configurable behavior.

It's certainly possible that there are applications out there that are (perhaps unwittingly) relying on shallow merging of shared props.

Updated the PR to make deep merging opt-in!