question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deep `stuff` merge

See original GitHub issue

Describe the problem

In the section about stuff returned by load in the docs, it is stated that:

This will be merged with any existing stuff and passed to the load functions of subsequent layout and page components.

This “merging” behavior means that if you, for example, do the following in routes/__layout.svelte:

export function load() {
    return {
        stuff: {
            prop1: 'foo',
            prop2: 'bar',
        }
    };
}

and this in route/someRoute.svelte:

export function load() {
    return {
        stuff: {
            prop2: 'something entirely different',
        }
    };
}

The resulting stuff would be:

{
    prop1: 'foo',
    prop2: 'something entirely different',
}

However, I noticed that SvelteKit only performs a “shallow” merge as opposed to a “deep” merge on stuff. This means that if your stuff has multiple levels of depth, the merging wouldn’t take place for the deeper objects. For example: routes/__layout.svelte:

export function load() {
    return {
        stuff: {
            footer: {
                show: true,
                layout: 'normal',
            }
        },
    };
}

route/someRoute.svelte:

export function load() {
    return {
        stuff: {
            footer: {
                layout: 'wide',
            }
        },
    };
}

The resulting stuff would be:

{
    footer: {
        layout: 'wide',
    }
}

What I expected was:

{
    footer: {
        show: true,
        layout: 'wide',
    }
}

Describe the proposed solution

Do a deep merge on stuff instead.

Alternatives considered

Currently, you have no choice but to keep the stuff object completely flat if you want to take advantage of the merging behavior.

Importance

would make my life easier

Additional Information

If you decide against this, I think it should at least be noted in the docs that the merging on stuff is shallow and is only performed on the first level of depth, so that users won’t assume otherwise.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
UltraCakeBakerycommented, Feb 17, 2022

Agree with @Conduitry — this absolutely belongs in userland, not in the framework:

export function load({ stuff }) {
    return {
        stuff: {
            footer: {
                ...stuff.footer,
                layout: 'wide',
            }
        },
    };
}

The people of User Land could also do delete stuff.footer before merging. We are talking about “stuff” which is added upon as it travels down the chain. To me it makes sense new stuff gets deep merged.

Besides that, svelte-kit could benefit from some more utilities. Making a deep merger function one of them isn’t such bad idea imo.

2reactions
aradalvandcommented, Feb 9, 2022

I’m +1 for documenting it, -1 for making it a deep merge.

Okay, could you elaborate a bit on why you’re against making it a deep merge? Would that be problematic? Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to deep merge instead of shallow merge? - Stack Overflow
Things to consider before writing your function. First, make sure you understand what a Javascript object is. In Javascript, an object is made ......
Read more >
deepMerge.js - The Vanilla JS Toolkit
deepMerge.js. Deep merge two or more objects together. Works in all modern browsers, and at least back to IE9. Demo on CodePen ...
Read more >
Merge and Deep Merge — and Why One Day You'll Find The ...
Enter Deep Merge. Rails comes with a deep_merge method courtesy of ActiveSupport. Returns a new hash with self and other_hash merged ...
Read more >
merge-anything - npm
I was looking for: a simple merge function like Object.assign() but deep; supports merging of nested properties; supports TypeScript: the type ...
Read more >
Deep Cuts from 30 Years of Merge Records - Bandcamp Daily
When Mac McCaughan and Laura Ballance co-founded Merge Records in 1989, ... 'Hey, you guys are doing cool stuff with these 7-inches.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found