a:2:{i:0;a:1:{s:4:"data";a:2:{s:7:"entries";a:10:{i:0;a:6:{s:5:"title";s:27:"Using localStorage in Remix";s:4:"slug";s:27:"using-localstorage-in-remix";s:2:"id";s:4:"2477";s:10:"typeHandle";s:4:"blog";s:4:"body";s:1581:"

I wanted to get some more experience working with GraphQL mutations, so I worked on a new feature for my site that enabled a user to “like” a blog article. I ran into a little gotcha that is kind of obvious now that I know the solution but wasn’t obvious at the time.

I didn’t think I needed to be fancy and store the “like” in the database and associate with the user, so I decided that tracking whether the user had liked the article in localStorage was enough and then keep track of the total article like count in the database. Seems easy enough right?

The Problem

This was my first approach in retrieving whether or not the user had liked the article from localStorage:

And this is the error that I was getting:

ReferenceError: window is not defined

The Fix

At first I was 🤔, but the fix is super simple and totally makes sense. Remix renders on the server, but I wanted to run this in the browser, so I simply needed to wrap it in a useEffect:

This is actually captured in the Remix docs, but it was kind of buried.

";s:10:"bodyBlocks";a:7:{i:0;a:3:{s:2:"id";s:4:"2640";s:10:"typeHandle";s:4:"text";s:4:"text";s:884:"

I wanted to get some more experience working with GraphQL mutations, so I worked on a new feature for my site that enabled a user to “like” a blog article. I ran into a little gotcha that is kind of obvious now that I know the solution but wasn’t obvious at the time.

I didn’t think I needed to be fancy and store the “like” in the database and associate with the user, so I decided that tracking whether the user had liked the article in localStorage was enough and then keep track of the total article like count in the database. Seems easy enough right?

The Problem

This was my first approach in retrieving whether or not the user had liked the article from localStorage:

";}i:1;O:8:"stdClass":0:{}i:2;a:3:{s:2:"id";s:4:"2642";s:10:"typeHandle";s:4:"text";s:4:"text";s:48:"

And this is the error that I was getting:

";}i:3;O:8:"stdClass":0:{}i:4;a:3:{s:2:"id";s:4:"2644";s:10:"typeHandle";s:4:"text";s:4:"text";s:232:"

The Fix

At first I was 🤔, but the fix is super simple and totally makes sense. Remix renders on the server, but I wanted to run this in the browser, so I simply needed to wrap it in a useEffect:

";}i:5;O:8:"stdClass":0:{}i:6;a:3:{s:2:"id";s:4:"2646";s:10:"typeHandle";s:4:"text";s:4:"text";s:173:"

This is actually captured in the Remix docs, but it was kind of buried.

";}}}i:1;a:6:{s:5:"title";s:55:"Setting Up Live Preview with Craft CMS in Headless Mode";s:4:"slug";s:55:"setting-up-live-preview-with-craft-cms-in-headless-mode";s:2:"id";s:4:"2266";s:10:"typeHandle";s:4:"blog";s:4:"body";s:2823:"

In my previous post, I talked about transitioning my site from being powered by Craft to being powered by Remix and a GraphQL endpoint coming from Craft. One of the best features of Craft is Live Preview, and it took a little configuration to get this working on my new Remix powered site.

Remix Configuration

These were specific changes that I needed to make on the Remix side, but really it would be the same for any GraphQL request. As Andrew Welch breaks down in his article, the key is passing the token parameter back that Craft included in the URL.

In the loader function of my blog post route, I call a helper function to setup the GraphQL client, and I pass the request to it:

And then in my helper function, I take that request, grab the URL params that I want to include, and add those to my GraphQL request endpoint URL:

It wasn’t totally necessary to include the x-craft-preview and x-craft-live-preview parameters, but I figured it couldn’t hurt.

Craft Configuration

Now with the Remix configuration in place, I just needed to make two small changes to Craft to get it working correctly. First, I had to update the preview target for my Blog section to point to my Remix site:

Setting the preview target in my Blog Section

Then, I had to update the config/general.php to allow iframe requests from my Remix domain:

Voilà, Live Preview

And just like that, I had live preview working.

";s:10:"bodyBlocks";a:7:{i:0;a:3:{s:2:"id";s:4:"2772";s:10:"typeHandle";s:4:"text";s:4:"text";s:999:"

In my previous post, I talked about transitioning my site from being powered by Craft to being powered by Remix and a GraphQL endpoint coming from Craft. One of the best features of Craft is Live Preview, and it took a little configuration to get this working on my new Remix powered site.

Remix Configuration

These were specific changes that I needed to make on the Remix side, but really it would be the same for any GraphQL request. As Andrew Welch breaks down in his article, the key is passing the token parameter back that Craft included in the URL.

In the loader function of my blog post route, I call a helper function to setup the GraphQL client, and I pass the request to it:

";}i:1;O:8:"stdClass":0:{}i:2;a:3:{s:2:"id";s:4:"2774";s:10:"typeHandle";s:4:"text";s:4:"text";s:153:"

And then in my helper function, I take that request, grab the URL params that I want to include, and add those to my GraphQL request endpoint URL:

";}i:3;O:8:"stdClass":0:{}i:4;a:3:{s:2:"id";s:4:"2776";s:10:"typeHandle";s:4:"text";s:4:"text";s:706:"

It wasn’t totally necessary to include the x-craft-preview and x-craft-live-preview parameters, but I figured it couldn’t hurt.

Craft Configuration

Now with the Remix configuration in place, I just needed to make two small changes to Craft to get it working correctly. First, I had to update the preview target for my Blog section to point to my Remix site:

Setting the preview target in my Blog Section

Then, I had to update the config/general.php to allow iframe requests from my Remix domain:

";}i:5;O:8:"stdClass":0:{}i:6;a:3:{s:2:"id";s:4:"2778";s:10:"typeHandle";s:4:"text";s:4:"text";s:235:"

Voilà, Live Preview

And just like that, I had live preview working.

";}}}i:2;a:6:{s:5:"title";s:32:"Why I’m So Excited About Remix";s:4:"slug";s:29:"why-im-so-excited-about-remix";s:2:"id";s:4:"2140";s:10:"typeHandle";s:4:"blog";s:4:"body";s:6064:"

I’ve been building on the web for a long time. There’s a part of me that still has a bit of an old school mentality, and it’s hard for me to get really excited about new technology. Sure React was cool when it came out for its state management, but I didn’t see it as the be-all and end-all for building on the web. I really like Craft CMS for building CMS powered sites since I have full control over the HTML being sent to the browser, and it’s really easy to extend for complex functionality, but it can be a bit complex to setup for beginners and with additional complexity can come performance issues. So it’s been a while since I’ve seen something to be really excited about.

Enter Remix.

Remix is an interesting mix of old school techniques along with modern technology. Ryan Florence actually sums it up perfectly as to where Remix fits in.

Forget “full stack”. Remix is center stack.

It’s an HTTP tunnel between interactivity and business logic. It’s the part of the stack that no tool before it has solved, not completely.

I think I finally know how to talk about Remix.

— Ryan Florence (@ryanflorence) January 15, 2022

I was able to easily switch my site that was fully powered by Craft to utilize Craft only as a GraphQL endpoint and Remix powering the rest. The two big things I needed to figure out were: how to get data from the GraphQL endpoint, and then how to process input from a user (for my contact form). I would definitely recommend reading through the Remix docs and the two tutorials, but if you can understand these two big pictures things, you’ll have enough to get started with Remix and go deeper once you get into it.

Getting GraphQL Data (aka Loaders)

Remix provides a loader hook that runs on the server to hydrate your component. In this simple example, I run a GraphQL request and return JSON with the results of that request in the loader function.

Then in my Index component, I access the data from the loader hook with useLoaderData(), and my component is now hydrated with that data. In MVC style of thinking, think of the loader hook as the controller processing the page load request, populating with data from the model, and then passing to the view.

The great part about Remix is that this doesn’t have to be specifically a GraphQL request. This data can come from anywhere. The Jokes tutorial in the Remix docs even thoroughly walks through how you can manage all this data in Prisma.

Processing User Input (aka Actions)

Ok great, so we’ve got data onto the page, but now we need to be able to handle user input. Remix also provides an action hook to process an action request (ex: a form submission). In my case, I have a contact form that sends me an email via SendGrid after submission.

If you don’t include an action attribute on your form, it will automatically submit to the same page. Again, in MVC thinking, the action would again be equivalent to the controller taking the request input and processing it. But honestly, this takes me back 15 years when form actions would point directly to PHP files.

Bonus: Progressive Enhancement

Since Remix is running on the server, you can turn off JavaScript and everything still works great. Data still loads, form actions still process; it’s beautiful. This is not running loaders and actions on the client side; it’s running them on the server. This isn’t an accident, the Remix team is carefully considering the UX.

User experience (UX) and developer experience (DX) are both important. UX is more important than DX.@remix_run has taught me: It's easier to start with a great UX and work toward a good DX than it is to start with a great DX and work toward a good UX.

— Kent C. Dodds 💿 (@kentcdodds) January 15, 2022

Bonus: So Speedy

Did I mention how fast everything renders? I don’t tend to put too much stock into Lighthouse scores since they seem so random, but Remix crushes Lighthouse without me having to invest any additional effort into improving performance.

Give it a Shot

Remix is built on the native Fetch API, so it can run anywhere. While Remix is relatively new, it’s certainly production ready, and there is a dedicated team behind it. If you aren’t ready to dive into Remix yet, at least keep your eye on it because this is a game changer.

";s:10:"bodyBlocks";a:5:{i:0;a:3:{s:2:"id";s:4:"2942";s:10:"typeHandle";s:4:"text";s:4:"text";s:2413:"

I’ve been building on the web for a long time. There’s a part of me that still has a bit of an old school mentality, and it’s hard for me to get really excited about new technology. Sure React was cool when it came out for its state management, but I didn’t see it as the be-all and end-all for building on the web. I really like Craft CMS for building CMS powered sites since I have full control over the HTML being sent to the browser, and it’s really easy to extend for complex functionality, but it can be a bit complex to setup for beginners and with additional complexity can come performance issues. So it’s been a while since I’ve seen something to be really excited about.

Enter Remix.

Remix is an interesting mix of old school techniques along with modern technology. Ryan Florence actually sums it up perfectly as to where Remix fits in.

Forget “full stack”. Remix is center stack.

It’s an HTTP tunnel between interactivity and business logic. It’s the part of the stack that no tool before it has solved, not completely.

I think I finally know how to talk about Remix.

— Ryan Florence (@ryanflorence) January 15, 2022

I was able to easily switch my site that was fully powered by Craft to utilize Craft only as a GraphQL endpoint and Remix powering the rest. The two big things I needed to figure out were: how to get data from the GraphQL endpoint, and then how to process input from a user (for my contact form). I would definitely recommend reading through the Remix docs and the two tutorials, but if you can understand these two big pictures things, you’ll have enough to get started with Remix and go deeper once you get into it.

Getting GraphQL Data (aka Loaders)

Remix provides a loader hook that runs on the server to hydrate your component. In this simple example, I run a GraphQL request and return JSON with the results of that request in the loader function.

";}i:1;O:8:"stdClass":0:{}i:2;a:3:{s:2:"id";s:4:"2944";s:10:"typeHandle";s:4:"text";s:4:"text";s:1127:"

Then in my Index component, I access the data from the loader hook with useLoaderData(), and my component is now hydrated with that data. In MVC style of thinking, think of the loader hook as the controller processing the page load request, populating with data from the model, and then passing to the view.

The great part about Remix is that this doesn’t have to be specifically a GraphQL request. This data can come from anywhere. The Jokes tutorial in the Remix docs even thoroughly walks through how you can manage all this data in Prisma.

Processing User Input (aka Actions)

Ok great, so we’ve got data onto the page, but now we need to be able to handle user input. Remix also provides an action hook to process an action request (ex: a form submission). In my case, I have a contact form that sends me an email via SendGrid after submission.

";}i:3;O:8:"stdClass":0:{}i:4;a:3:{s:2:"id";s:4:"2946";s:10:"typeHandle";s:4:"text";s:4:"text";s:2044:"

If you don’t include an action attribute on your form, it will automatically submit to the same page. Again, in MVC thinking, the action would again be equivalent to the controller taking the request input and processing it. But honestly, this takes me back 15 years when form actions would point directly to PHP files.

Bonus: Progressive Enhancement

Since Remix is running on the server, you can turn off JavaScript and everything still works great. Data still loads, form actions still process; it’s beautiful. This is not running loaders and actions on the client side; it’s running them on the server. This isn’t an accident, the Remix team is carefully considering the UX.

User experience (UX) and developer experience (DX) are both important. UX is more important than DX.@remix_run has taught me: It's easier to start with a great UX and work toward a good DX than it is to start with a great DX and work toward a good UX.

— Kent C. Dodds 💿 (@kentcdodds) January 15, 2022

Bonus: So Speedy

Did I mention how fast everything renders? I don’t tend to put too much stock into Lighthouse scores since they seem so random, but Remix crushes Lighthouse without me having to invest any additional effort into improving performance.

Give it a Shot

Remix is built on the native Fetch API, so it can run anywhere. While Remix is relatively new, it’s certainly production ready, and there is a dedicated team behind it. If you aren’t ready to dive into Remix yet, at least keep your eye on it because this is a game changer.

";}}}i:3;a:6:{s:5:"title";s:37:"What I Love & Hate About Tailwind CSS";s:4:"slug";s:35:"what-i-love-hate-about-tailwind-css";s:2:"id";s:4:"2091";s:10:"typeHandle";s:15:"externalArticle";s:4:"body";s:120:"

As a long time skeptic of Tailwind CSS, I’ve finally given it a try and discovered some things I love and hate.

";s:7:"website";s:67:"https://www.viget.com/articles/what-i-love-hate-about-tailwind-css/";}i:4;a:6:{s:5:"title";s:43:"How We Prevent Leaky Templates in Craft CMS";s:4:"slug";s:43:"how-we-prevent-leaky-templates-in-craft-cms";s:2:"id";s:4:"2089";s:10:"typeHandle";s:15:"externalArticle";s:4:"body";s:87:"

Prevent the flood of leaky templates in Craft CMS with just a little bit of PHP.

";s:7:"website";s:75:"https://www.viget.com/articles/how-we-prevent-leaky-templates-in-craft-cms/";}i:5;a:6:{s:5:"title";s:48:"Level Up with Craft CMS: Know When to Ditch Twig";s:4:"slug";s:47:"level-up-with-craft-cms-know-when-to-ditch-twig";s:2:"id";s:3:"858";s:10:"typeHandle";s:15:"externalArticle";s:4:"body";s:121:"

If you notice your Twig templates are getting overly complex, it may be time to extend Craft with a custom Module.

";s:7:"website";s:79:"https://www.viget.com/articles/level-up-with-craft-cms-know-when-to-ditch-twig/";}i:6;a:6:{s:5:"title";s:37:"Why You Should Update to Craft 3 ASAP";s:4:"slug";s:37:"why-you-should-update-to-craft-3-asap";s:2:"id";s:3:"854";s:10:"typeHandle";s:15:"externalArticle";s:4:"body";s:194:"

Updating to Craft 3 means new features for your site with improved performance and security. This latest version was released earlier this year, and you should update as soon as possible!

";s:7:"website";s:69:"https://www.viget.com/articles/why-you-should-update-to-craft-3-asap/";}i:7;a:6:{s:5:"title";s:36:"Managing CSS & JS in an HTTP/2 World";s:4:"slug";s:34:"managing-css-js-in-an-http-2-world";s:2:"id";s:3:"813";s:10:"typeHandle";s:15:"externalArticle";s:4:"body";s:669:"

We have been hearing about HTTP/2 for years now. We've even blogged a little bit about it. But we hadn't really done much with it. Until now. On a few recent projects, I made it a goal to use HTTP/2 and figure out how to best utilize multiplexing. This post isn't necessarily going to cover why you should use HTTP/2, but it's going to discuss how I've been managing CSS & JS to account for this paradigm shift.

";s:7:"website";s:53:"https://www.viget.com/articles/managing-css-js-http-2";}i:8;a:6:{s:5:"title";s:27:"Craft Color Swatches Plugin";s:4:"slug";s:27:"craft-color-swatches-plugin";s:2:"id";s:3:"811";s:10:"typeHandle";s:15:"externalArticle";s:4:"body";s:607:"

The control that Craft can provide a user is what makes it stand out as a content management system. But sometimes we want to limit what a user can choose from. Craft has a built-in Color field which allows a user to select any color from a color picker. There are times when we only want a user to choose from a select number of colors though. Previously we have done this by using a dropdown with a list of colors, but I decided to build a plugin to allow a user to select from an admin-defined set of colors.

";s:7:"website";s:58:"https://www.viget.com/articles/craft-color-swatches-plugin";}i:9;a:6:{s:5:"title";s:37:"Responsive Images with srcset & Craft";s:4:"slug";s:35:"responsive-images-with-srcset-craft";s:2:"id";s:3:"810";s:10:"typeHandle";s:15:"externalArticle";s:4:"body";s:438:"

Tommy recently wrote about responsive background images in Craft, but I wanted to follow up about how we used the <img> element and srcset to build responsive images on this very site (powered by Craft).

";s:7:"website";s:66:"https://www.viget.com/articles/responsive-images-with-srcset-craft";}}s:5:"total";i:270;}}i:1;O:25:"yii\caching\TagDependency":3:{s:4:"tags";a:26:{i:0;s:7:"element";i:1;s:29:"element::craft\elements\Entry";i:2;s:40:"element::craft\elements\Entry::section:4";i:3;s:35:"element::craft\elements\MatrixBlock";i:4;s:41:"element::craft\elements\MatrixBlock::2772";i:5;s:41:"element::craft\elements\MatrixBlock::2942";i:6;s:41:"element::craft\elements\MatrixBlock::2640";i:7;s:41:"element::craft\elements\MatrixBlock::2943";i:8;s:41:"element::craft\elements\MatrixBlock::2773";i:9;s:41:"element::craft\elements\MatrixBlock::2641";i:10;s:41:"element::craft\elements\MatrixBlock::2944";i:11;s:41:"element::craft\elements\MatrixBlock::2774";i:12;s:41:"element::craft\elements\MatrixBlock::2642";i:13;s:41:"element::craft\elements\MatrixBlock::2945";i:14;s:41:"element::craft\elements\MatrixBlock::2775";i:15;s:41:"element::craft\elements\MatrixBlock::2643";i:16;s:41:"element::craft\elements\MatrixBlock::2776";i:17;s:41:"element::craft\elements\MatrixBlock::2644";i:18;s:41:"element::craft\elements\MatrixBlock::2946";i:19;s:41:"element::craft\elements\MatrixBlock::2777";i:20;s:41:"element::craft\elements\MatrixBlock::2645";i:21;s:41:"element::craft\elements\MatrixBlock::2778";i:22;s:41:"element::craft\elements\MatrixBlock::2646";i:23;s:29:"element::craft\elements\Asset";i:24;s:35:"element::craft\elements\Asset::2283";i:25;s:7:"graphql";}s:4:"data";a:26:{s:40:"CraftCMSce35088bdfe0816226cd17fd051a4803";s:21:"0.46162700 1707493692";s:40:"CraftCMS2743a789e8993267a348eee1ee7e4450";s:21:"0.34758900 1706282441";s:40:"CraftCMS2ac34726f299f7e18e449d8e536c67f8";s:21:"0.96549800 1705677261";s:40:"CraftCMSd9961e9460af421f810dce3dc85f38f9";s:21:"0.96283300 1706283345";s:40:"CraftCMSf01e95333f914a83e9f4a3a87860c9c8";s:21:"0.07136200 1706309071";s:40:"CraftCMS74fbda636551db775f159218100df071";s:21:"0.53658200 1706368644";s:40:"CraftCMS36408baf6977a45751e0965cdfd7eb13";s:21:"0.43664400 1706308690";s:40:"CraftCMS6989bd76a85920bd4e0847682f0d80ad";s:21:"0.53658200 1706368644";s:40:"CraftCMS879865bafcd5568b0ebf9bd1329e1bde";s:21:"0.07136200 1706309071";s:40:"CraftCMS937f7c59ce6a64c744f12965261771e1";s:21:"0.43664400 1706308690";s:40:"CraftCMScaa76847c2ed8d3b53528ac4f7e8792f";s:21:"0.53658200 1706368644";s:40:"CraftCMS1a84384d237a229b582f6d5c3b286aac";s:21:"0.07136200 1706309071";s:40:"CraftCMS2e5da15f9409549825a41b97199fccfa";s:21:"0.43664400 1706308690";s:40:"CraftCMSace6fadba39f4590bf679370fac5c07e";s:21:"0.53658200 1706368644";s:40:"CraftCMScafaeba1cf0c05d6fe859f3b6278efc5";s:21:"0.07136200 1706309071";s:40:"CraftCMSa90a259b1bc12c4fe8836d475f6f3e28";s:21:"0.43664400 1706308690";s:40:"CraftCMSfcca9552d5f49bac340d9bd970d17779";s:21:"0.07136200 1706309071";s:40:"CraftCMS00cd6e4e7f131f004dcbad857c274ec8";s:21:"0.43664400 1706308690";s:40:"CraftCMS0f5d316166e1d14f747d218209d25398";s:21:"0.53658200 1706368644";s:40:"CraftCMS1effda415785c15eb06e01cb6ba6d1df";s:21:"0.07136200 1706309071";s:40:"CraftCMS649adc96e40b8c3cd59cdfcd60a9b54e";s:21:"0.43664400 1706308690";s:40:"CraftCMSff036eb30387b5f201f43f395a54dabf";s:21:"0.07136200 1706309071";s:40:"CraftCMSea9f2b86f58ee2571e4d12df51a685cc";s:21:"0.43664400 1706308690";s:40:"CraftCMS656ba125f2735c9321627f00daa5a4cd";s:21:"0.21062500 1707493467";s:40:"CraftCMS559b51e8f9dfd99ac11bb6131c8b6595";s:21:"0.84443800 1705937209";s:40:"CraftCMS3817d4a648fcfac939af46605323feb0";s:21:"0.44505500 1681499442";}s:8:"reusable";b:0;}}