<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bartosz Trzos</title>
    <description>The latest articles on DEV Community by Bartosz Trzos (@remotelydev).</description>
    <link>https://dev.to/remotelydev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F244877%2Fe89489fb-9d00-406c-9582-19b78134402e.png</url>
      <title>DEV Community: Bartosz Trzos</title>
      <link>https://dev.to/remotelydev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/remotelydev"/>
    <language>en</language>
    <item>
      <title>Yet another JAM stack blog</title>
      <dc:creator>Bartosz Trzos</dc:creator>
      <pubDate>Thu, 02 Jan 2020 17:52:30 +0000</pubDate>
      <link>https://dev.to/remotelydev/yet-another-jam-stack-blog-1f20</link>
      <guid>https://dev.to/remotelydev/yet-another-jam-stack-blog-1f20</guid>
      <description>&lt;p&gt;&lt;em&gt;This is my first post 🎉 please leave a comment on it, and my site and don't be gentle. Cheers!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The purpose
&lt;/h2&gt;

&lt;p&gt;I needed a site where I could publish some posts to &lt;a href="https://www.swyx.io/writing/learn-in-public/"&gt;learn in public&lt;/a&gt; and keep my knowledge in one place. I often find myself googling stuff I used to develop something while I have to do something similar. I want to write about my process and keep it on &lt;a href="https://trzos.dev/"&gt;trzos.dev&lt;/a&gt;, but first I need an MVP, therefore the blog.&lt;/p&gt;

&lt;h2&gt;
  
  
  The stack
&lt;/h2&gt;

&lt;p&gt;I choose, Sapper, Tailwind, Dev.to. Let's delve a bit into each of them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://sapper.svelte.dev/"&gt;Sapper&lt;/a&gt;&lt;/strong&gt; - It's Svelte universal app framework. After I watched &lt;a href="https://www.youtube.com/watch?v=gJ2P6hGwcgo"&gt;Rich Harris talk&lt;/a&gt; I thought I'd love to work with it. The concept is great, it offers amazing (and fun) developer experience, and I love to experiment with something new. I chose Sapper over Svelte for better SEO and static site generation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://tailwindcss.com/"&gt;Tailwind&lt;/a&gt;&lt;/strong&gt; - It's a utility first CSS framework from Adam Wathan. It looks a bit ugly in the code at first, but it really makes it easier to maintain and it's just amazing how well it works with component-based frameworks. Utilities for animations and transitions coming soon as well. Can't wait!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://dev.to/"&gt;Dev.to&lt;/a&gt;&lt;/strong&gt; - I choose this to have a platform to write on, and I like their policies. I guess you already know that they are better than others though, as you're probably a dev.to reader 😅. Bonus points for Markdown editor and API. Feels like a good place for developers 🤓&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;First, we need to create an app with Sapper. Fortunately someone prepared a script for us that generates it automatically. Just go to your projects folder and run one of below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# for Rollup
npx degit "sveltejs/sapper-template#rollup" my-app
# for webpack
npx degit "sveltejs/sapper-template#webpack" my-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can choose &lt;a href="https://rollupjs.org/guide/en/"&gt;Rollup&lt;/a&gt; or &lt;a href="https://webpack.js.org/"&gt;Webpack&lt;/a&gt;. I choose Webpack because I'm more familiar with it, but it's totally up to you. They are both good module bundlers.&lt;/p&gt;

&lt;p&gt;Next, we need to run the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd my-app

npm install
npm run dev &amp;amp; open http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You're good if you can see Borat on the main page. I told you Svelte is fun 😄 &lt;/p&gt;

&lt;p&gt;Next, we would like to add Tailwind. I used &lt;a href="https://github.com/tailwindcss/setup-examples/tree/master/examples/sapper"&gt;this&lt;/a&gt; tutorial, and it works well. Just keep in mind that you can use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tailwind init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;instead of the command that runs a script from &lt;code&gt;/node_modules/&lt;/code&gt;. Does the same thing, it's just easier to type in. PostCSS config file in this example looks for &lt;code&gt;tailwind.js&lt;/code&gt; instead of &lt;code&gt;tailwind.config.js&lt;/code&gt;. Watch out and fix one or the other.&lt;br&gt;
I went with PurgeCSS and I recommend you to do so as well. It saves some kB on the app as it shaves off unused classes. That's brilliant with Tailwind as you can have all generated utility classes and drop unused ones on production 💪🏻&lt;br&gt;
You will know that Tailwind is installed first by the fact that Borat image went left for some reason, and that you can actually use Tailwind classes. I'd recommend to remove the &lt;code&gt;global.css&lt;/code&gt; file from &lt;code&gt;/static&lt;/code&gt; folder and from &lt;code&gt;template.html&lt;/code&gt;. You probably won't need this when you use Tailwind.&lt;/p&gt;
&lt;h2&gt;
  
  
  The blog
&lt;/h2&gt;

&lt;p&gt;Sapper boilerplate comes with a blog example. In the &lt;code&gt;routes&lt;/code&gt; folder, you will find the &lt;code&gt;blog&lt;/code&gt; directory, which represents your &lt;code&gt;/blog&lt;/code&gt; route. I removed all &lt;code&gt;.js&lt;/code&gt; files there as their purpose is to mock API calls for posts and we will use &lt;code&gt;dev.to&lt;/code&gt; API. You should be left with &lt;code&gt;blog/index.svelte&lt;/code&gt; and &lt;code&gt;blog/[slug].svelte&lt;/code&gt;, which are for the blog feed and each blog post accordingly.&lt;br&gt;
Let's focus on &lt;code&gt;index.svelte&lt;/code&gt;. First we need to call &lt;code&gt;dev.to&lt;/code&gt; API instead of the local mock.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script context="module"&amp;gt;
  export function preload({ params, query }) {
    return this.fetch(`https://dev.to/api/articles/?username=remotelydev`)
      .then(r =&amp;gt; r.json())
      .then(posts =&amp;gt; {
        return { posts };
      });
  }
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replacing first script tag with one above should do the trick. Notice that I target my publushed posts. Make sure to use you username.&lt;/p&gt;

&lt;p&gt;Now on the blog page, you should see a list of my/your posts &lt;strong&gt;published&lt;/strong&gt; on &lt;code&gt;dev.to&lt;/code&gt;. The link is broken though, let's fix that!&lt;/p&gt;

&lt;p&gt;Still in &lt;code&gt;blog/index.svelte&lt;/code&gt; update the link in the list to pass id in the query like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a rel="prefetch" href="blog/{post.slug}?id={post.id}"&amp;gt;
  &amp;lt;h2 class="text-black"&amp;gt;{post.title}&amp;lt;/h2&amp;gt;
&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now clicking the link will also pass the &lt;code&gt;post.id&lt;/code&gt; which is necessary to fetch the exact post. It's not the best solution, but it was first and working one I could think of. Leave a comment with a better idea, and I'll update the post. 👌🏻&lt;/p&gt;

&lt;p&gt;Let's go to &lt;code&gt;blog/[slug].svelte&lt;/code&gt; and update the first script tag with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script context="module"&amp;gt;
  export async function preload({ params, query }) {
    // the `slug` parameter is available because
    // this file is called [slug].svelte
    // const res = await this.fetch(`blog/${params.slug}.json`);
    const res = await this.fetch(`https://dev.to/api/articles/${query.id}`);
    const post = await res.json();

    if (res.status === 200) {
      return { post };
    } else {
      this.error(res.status, post.message);
    }
  }
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See how I used the post &lt;code&gt;id&lt;/code&gt; to get proper post? It's needed to get exact your post. Sapper is cool enough to make the call on server side.&lt;/p&gt;

&lt;p&gt;All left to do is to use proper content property from the response. For &lt;code&gt;dev.to&lt;/code&gt; API it's &lt;code&gt;post.body_html&lt;/code&gt; instead of &lt;code&gt;post.html&lt;/code&gt; from the boilerplate. And you have your blog with Svelte and Dev.to 🎉 Just add some personal info, styles and write your posts! Cheers! 👋🏻&lt;/p&gt;

&lt;p&gt;P.S.: I use Sapper generate functionality. It walks through all your routes and links and generates static site from it. Pretty neat! I like this particularly because you can have a static site if you don't need to keep users logged in, but if you'll ever decide that you want to you can serve the app as usual. &lt;/p&gt;

</description>
      <category>helloworld</category>
      <category>sapper</category>
      <category>tailwindcss</category>
      <category>blog</category>
    </item>
  </channel>
</rss>
