{{article.title}}
13 |This is a fan site dedicated to the Loki TV series. It also serves as a tutorial of how to build a fansite using Nuxt, Cosmic JS, imgix, Tailwind CSS, & Vercel.
15 |
27 |
28 | Each item you add in the homes bucket will reflect a different section you can click into on your home page. It's important to think about the organization of your site now, because this is what will enable that. In the example, I make a Loki site and created three sections:
29 |
30 | * Episodes (a section to look at each episode from the show)
31 | * Lokis (a section to look at each Loki variant from the show)
32 | * About (an about page)
33 |
34 | These sections have very limited info needed. Just a title and an image. Their names are important, because this will become the routes for each section in your nuxt app that you will be updating.
35 |
36 |
37 |
38 | You will now want to create Cosmic buckets for each section you made. In the template, you already have the three I created:
39 |
40 | * episodes
41 | * heroes
42 | * homes
43 |
44 | You may want to edit or add additional ones. These buckets will allow you to add content to each of those sections you started creating.
45 |
46 | When you create a cosmic bucket, select them as a multiple bucket at the top. This is because you are going to enter multiple entries.
47 |
48 |
49 |
50 | Then leave the default settings of title, slug, content alone. You will then most likely want to add an image to the metadata area and anything else you may want to display. In my examples, I added a heroimage and an episode_number.
51 |
52 |
53 |
54 | You can go ahead and finish making these buckets and add some content to them.
55 |
56 | ## Apollo GraphQL Queries with Cosmic
57 |
58 | Once you have some content in your Cosmic buckets, you now need to make a few updates to the Nuxt app in order to display your content. You will need to update the GraphQL Queries we are doing to Cosmic. For these queries, I am using Apollo. You will find 5 Queries in a apollo/queries folder.
59 |
60 |
61 |
62 | Here is an example of what one of the queries looks like:
63 |
64 | ```
65 | {
66 | getObjects(
67 | bucket_slug: "",
68 | read_key: "",
69 | input: {
70 | query: { type: "episodes" }
71 | }
72 | ) {
73 | objects {
74 | id
75 | content
76 | title
77 | metadata
78 | slug
79 | }
80 | }
81 | }
82 | ```
83 |
84 | You will need to update the bucket_slug and the read_key. Also, if you are adding additional or Modifying Cosmic Buckets, you would add the name of the bucket to the query type. You can find the bucket_slug and read_key in your Cosmic settings, API Access. We are not using the write_key, so the rest of these items are not a concern to publicly be seen. That's why they are not obscured in my screenshot or added to .env files or anything.
85 |
86 |
87 |
88 | If you do want to do a bit of a deeper dive on the GraphQL, Cosmic does have a simple GraphQL playground. Just click on one of your Cosmic Buckets, then next to the name of it, you will see Developer Tools. This can help you debug items and see what the values you are going to get from each query. The reason I am choosing to use the GraphQL is to ensure that I don't get large payloads by accessing all of the content of my Cosmic site on each page load. The GraphQL allows me to get pretty specific and only ask for certain content. In my example query above, I'm only asking for the id, content, title, slug, and metadata of the items in the episodes section. The metadata is the additional items we added, like the hero image or the episode number.
89 |
90 |
91 |
92 | Once you have filled in the bucket_slug and the read_key, your queries should look like this:
93 |
94 | ```
95 | {
96 | getObjects(
97 | bucket_slug: "loki-webinar-production",
98 | read_key: "TMNjyVSNbfE75hA7q92OTZex74Ye5NuIn20DRWNroYgRGj4y9Y",
99 | input: {
100 | query: {
101 | type: "episodes"
102 | }
103 | }) {
104 | objects {
105 | id
106 | content
107 | title
108 | metadata
109 | slug
110 | }
111 | }
112 | }
113 | ```
114 |
115 | You can also certainly use my bucket_slug and read_key if you would like to just see the content I did in my demo app.
116 |
117 | ## Deploy Via Vercel
118 |
119 | At this point, you actually don't need to make any additional changes. You can run the app locally using `yarn dev` or deploy it easily via Vercel or whatever tool you prefer. There are certainly additional tweeks we can make to the site which I will go over below if you would like as well. I have added a vercel.json file to this template, so it is extremely simple to deploy the site. I find it easiest to push the site to Github and connect your Github with Vercel. You can then just click `New Project` in Vercel and choose the git repo you pushed the project to. No other adjustments or settings need to be added. The settings of the vercel.json ensures everything will work smoothly.
120 |
121 | ## Customizations to Routes
122 |
123 | The routes are set-up for three paths in the pages folder of your nuxt-app. They are set up as:
124 |
125 | * Episodes
126 | * Lokis
127 | * About
128 |
129 | Depending on how many objects you created in your homes bucket and what they are called, you should modify these. In my example, I called them Lokis, Episodes, and About in my Cosmic bucket, which is why I changed the names of the folders in my pages section to be identical to those.
130 |
131 | If you open one of those folders (I opened episodes) you will see a index.vue and a _id.vue. The _id.vue is a dynamic route, for each of the individual objects I created in my episodes bucket, there will be a dynamic route to match those. No customization needs to happen with the _id.vue.
132 |
133 | For the index.vue, this would be the episodes page, which displays all of the objects you added to the episodes bucket. In the index.vue of the episodes folder, you will notice it is pretty empty.
134 |
135 | ```
136 |
137 |
235 | ```
236 |
237 | All I had to do was install the Vue SDK and change the img tag to an ix-img, a very easy way to generate great srcsets for my images.
238 |
239 | ## Optimizing my OG images
240 |
241 | Something you always want users to do is to share content from your website. This is a great opportunity for your content to be viewed by many other users that may have not found it. I love it when shared content is customized in some way to stand out. One way I love to do this is by optimizing the og:image and twitter:image. You can add watermarks or logos of your website to these images, maybe you can even overlay text on a nice overlay from your article on the image.
242 |
243 | That's going to be a lot of time in figma or photoshop to create all these customized images to be shared. But wait, all of the images are on imgix. imgix is practically a photoshop on the fly, with tons of APIs to add custom watermarks, blends, text, etc. I can actually create a nice function or component in Nuxt to programmatically append the metadata to each of the og:images for every article. If you go to the episode.vue component, you will see a component I reference their called `
266 |
267 |
268 | ## Conclusion
269 |
270 | Not only has this been a really fun site template to make, it's been great to further explore some advanced ways to use imgix to optimize the images. it is truly a powerful tool to have already included with your Cosmic accounts. Remember, there is a [tutorial video](https://youtu.be/YYMptR7Fpn4) which was done by Tony Spiro (CEO at Cosmic) and Tom Dale (Head of Customer Success at imgix). There is also an accompanying App in the [Cosmic Marketplace](https://www.cosmicjs.com/apps/nuxt-fan-site-with-responsive-images). I hope you do fun things with this app and my suggestions and look forward to helping with anything else you might need.
271 |
--------------------------------------------------------------------------------