<?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: Tushar saxena</title>
    <description>The latest articles on DEV Community by Tushar saxena (@tsaxena4k).</description>
    <link>https://dev.to/tsaxena4k</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%2F492575%2F4d38815c-4ce7-40de-bb25-b47c4397125f.jpg</url>
      <title>DEV Community: Tushar saxena</title>
      <link>https://dev.to/tsaxena4k</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tsaxena4k"/>
    <language>en</language>
    <item>
      <title>Custom Image Size Limiter &amp; Media Type Checker in Sitecore</title>
      <dc:creator>Tushar saxena</dc:creator>
      <pubDate>Fri, 21 Jul 2023 09:13:59 +0000</pubDate>
      <link>https://dev.to/tsaxena4k/custom-image-size-limiter-media-type-checker-in-sitecore-3809</link>
      <guid>https://dev.to/tsaxena4k/custom-image-size-limiter-media-type-checker-in-sitecore-3809</guid>
      <description>&lt;h2&gt;
  
  
  Ask
&lt;/h2&gt;

&lt;p&gt;The requirement was to limit image size on a image field in any template and also to restrict the type of image.&lt;/p&gt;

&lt;p&gt;For example: &lt;strong&gt;The image should be less then 1 MB &amp;amp; should be either png or jpg&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Since the ask was specific to an image field on a template and not to all image fields in general, the first thing that comes to any sitecore developer's mind is using a validation rule on that field inside your template it best suits the purpose. Yes! that is correct, but since there is no default validation rule to use we need to create our very own.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So, lets start by &lt;strong&gt;creating a validation rule item&lt;/strong&gt; for it within sitecore.

&lt;ul&gt;
&lt;li&gt;Go to &lt;code&gt;/sitecore/system/Settings/Validation Rules/Field Rules&lt;/code&gt;  in your sitecore content tree and insert a Validation Rule item by right clicking on the folder and choosing the insert option from there.&lt;/li&gt;
&lt;li&gt;You can name the item whatever you want to but for this post we are naming it as &lt;code&gt;image size limiter and type checker&lt;/code&gt; , you can fill in the &lt;code&gt;Title&lt;/code&gt;and the &lt;code&gt;Description&lt;/code&gt; with name of the item for now.&lt;/li&gt;
&lt;li&gt;Keep the &lt;code&gt;Type&lt;/code&gt; and the &lt;code&gt;Parameter&lt;/code&gt; field empty for now we will be filling this up later in the post.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Attach this custom validation rule item&lt;/strong&gt; on the image field.

&lt;ul&gt;
&lt;li&gt;Inside of your template item go to the section where you have your image field item defined. Click on the item.&lt;/li&gt;
&lt;li&gt;Now, scroll down a little on the right until you see the Validation Rule section. Attach your custom validation rule item we just created on all the four multilist that you see. It should look something like this.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnclvouaol9klw5tov9js.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnclvouaol9klw5tov9js.png" alt="Validation rules set for the image field"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, lets &lt;strong&gt;write code for our custom validation rule&lt;/strong&gt;.

&lt;ul&gt;
&lt;li&gt;Now, in your app's codebase in .Net create a class(.cs) file and name it &lt;code&gt;CustomImageSizeValidator&lt;/code&gt; in one of the project inside your solution. In my case it is inside &lt;code&gt;Adx.Foundation.SitecoreFields.Field_Validations&lt;/code&gt; where &lt;code&gt;Adx.soln&lt;/code&gt; is my solution's name.&lt;/li&gt;
&lt;li&gt;Let's create a basic structure of our validation rule file with all the required member functions.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Collections.Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Linq&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Web&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Sitecore.Data.Items&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Sitecore.Globalization&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Sitecore.Data.Fields&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Sitecore.Data.Validators&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Sitecore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Sitecore.Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;Adx.Foundation.SitecoreFields.Field_Validations&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomImageSizeValidator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;StandardValidator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;CustomImageSizeValidator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;get&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Image Size and Type Validator"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;ValidatorResult&lt;/span&gt; &lt;span class="nf"&gt;GetMaxValidatorResult&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;GetFailedResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ValidatorResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;ValidatorResult&lt;/span&gt; &lt;span class="nf"&gt;Evaluate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Field&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;GetField&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ValidatorResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Valid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;maxSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;GetMaxSizeFromParameters&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// fetch maxSize from the Parameters&lt;/span&gt;

      &lt;span class="n"&gt;List&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;extensions&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;GetExtensionsFromParameters&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

      &lt;span class="n"&gt;ImageField&lt;/span&gt; &lt;span class="n"&gt;imageField&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ImageField&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="c1"&gt;// Get the MediaItem from the ImageField&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;imageField&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;imageField&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MediaItem&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;MediaItem&lt;/span&gt; &lt;span class="n"&gt;mediaItem&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MediaItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;imageField&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MediaItem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// In case the extension parameter is not provided&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mediaItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Extension&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mediaItem&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;mediaItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;maxSize&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mediaItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Extension&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ValidatorResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Valid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mediaItem&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;mediaItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;maxSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Translate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The image is not of {0} type."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;","&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mediaItem&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mediaItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Extension&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Translate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The image size exceeds the allowed limit of {0}."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MainUtil&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FormatSize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxSize&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Translate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The image size exceeds the allowed limit of {0} and image is not of {1} type."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MainUtil&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FormatSize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxSize&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;","&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ValidatorResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Valid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;GetFailedResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ValidatorResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;GetMaxSizeFromParameters&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

      &lt;span class="c1"&gt;// Read the custom parameters from the validation rule item's Parameters field&lt;/span&gt;
      &lt;span class="n"&gt;SafeDictionary&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;GetParameters&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameters&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"maxSize"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"maxSize"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="c1"&gt;// If parsing fails, use a default value&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;1048576&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 1 MB&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetExtensionsFromParameters&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

      &lt;span class="n"&gt;List&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;extensions&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;();&lt;/span&gt;

      &lt;span class="c1"&gt;// Read the custom parameters from the validation rule item's Parameters field&lt;/span&gt;
      &lt;span class="n"&gt;SafeDictionary&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;GetParameters&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameters&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"extensions"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;extensions&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"extensions"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;','&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;ToList&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;SafeDictionary&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetParameters&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

      &lt;span class="c1"&gt;// Access the Parameters property of the base Validator to get the parameters defined in the validation rule item.&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GetParameters( )&lt;/strong&gt; inherits the base Parameter property which holds all the parameters defined in our validation rule items within sitecore.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GetMaxSizeFromParameters( )&lt;/strong&gt; method  is created to get the maxSize parameter from the Parameter field on our custom validation rule item within sitecore.&lt;/li&gt;
&lt;li&gt;Similarly, &lt;strong&gt;GetExtensionsFromParameters( )&lt;/strong&gt; method is created to get the extensions from the Parameter field on our custiom validation rule item within sitecore.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evaluate( )&lt;/strong&gt; method is called whenever an operation is performed on the image field to which our custom validation rule is attached.&lt;/li&gt;
&lt;li&gt;Now, that our logic is in place lets head back to sitecore and &lt;strong&gt;set the remaining fields on our custom validation rule item&lt;/strong&gt;.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Type&lt;/strong&gt;: In my case the namespace with the class name is &lt;code&gt;Adx.Foundation.SitecoreFields.Field_Validations.CustomImageSizeValidator&lt;/code&gt; fill yours accordingly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parameters&lt;/strong&gt;: &lt;code&gt;maxSize=1048576&amp;amp;extensions=png,jpg&amp;amp;Result=FatalError&lt;/code&gt; fill in the required parameters and I am using &lt;code&gt;Result=FatalError&lt;/code&gt; parameter to prevent user from saving the item if the validation fails.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;We are all set!&lt;/strong&gt; Now you can go to any item created from that template and try voilating the validations we just defined on the image field in it. &lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Note&lt;/u&gt;:&lt;/strong&gt; By default image field have the &lt;code&gt;alt&lt;/code&gt; text validation rule set up which takes severity and displays first. After adding the alt text upon saving you can see our validation displaying if there is any voilation. &lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;PS&lt;/strong&gt;: Thank you for following up I hope this was of some help if yes pls consider dropping a comment down below, It helps me be motivated to drive this through😄.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>sitecore</category>
      <category>jss</category>
      <category>imagesize</category>
      <category>validationrule</category>
    </item>
    <item>
      <title>Integrating Next.js with Leaflet.js + Mapbox</title>
      <dc:creator>Tushar saxena</dc:creator>
      <pubDate>Sat, 05 Dec 2020 08:16:21 +0000</pubDate>
      <link>https://dev.to/tsaxena4k/integrating-next-js-with-leaflet-js-mapbox-1351</link>
      <guid>https://dev.to/tsaxena4k/integrating-next-js-with-leaflet-js-mapbox-1351</guid>
      <description>&lt;p&gt;&lt;strong&gt;Do you want to include interactive maps in your &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Nextjs&lt;/a&gt; application?&lt;/strong&gt;Then you must have come across &lt;a href="https://leafletjs.com/" rel="noopener noreferrer"&gt;Leafletjs&lt;/a&gt;. Though Leafletjs is very simple to use, but when it comes to Server Side rendered(SSR) applications build with Nextjs it lacks a little which can be annoying at times. But don't you worry I've found a workaround for this. &lt;/p&gt;

&lt;p&gt;To set the scene let us first know 👇&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why Leafletjs?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Leaflet is the leading &lt;code&gt;open-source&lt;/code&gt; JavaScript library for &lt;code&gt;mobile-friendly&lt;/code&gt; interactive maps. Weighing just about 39 KB of JS, it has all the &lt;code&gt;mapping features&lt;/code&gt; most developers ever need.&lt;/p&gt;

&lt;p&gt;While Leaflet is meant to be as lightweight as possible, and focuses on a core set of features, an easy way to extend its functionality is to use third-party plugins. Thanks to the awesome community behind Leaflet, there are literally hundreds of nice plugins to choose from. We'll use one of those plugins in an example later in this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Tutorial&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Please note that in this tutorial, I am making the assumption that you already have an existing Next.js project up and running. If you don’t, please start by traversing the &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Next.js documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Install the required dependencies&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;npm i leaflet leaflet-defaulticon-compatibility leaflet-geosearch react-leaflet&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: if you use TypeScript, make sure you install &lt;code&gt;@types/leaflet&lt;/code&gt; otherwise you'll get compile errors on certain attributes used in the example.&lt;/p&gt;

&lt;p&gt;I'll explain the requirement of each as we use them further in the tutorial.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Creating the map component&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In your application create a &lt;strong&gt;map.jsx&lt;/strong&gt; file inside the component folder &lt;code&gt;./component/Map.jsx&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is important that this code is in a separate file from where it is embedded into your page because otherwise you'll get a &lt;em&gt;window undefined error&lt;/em&gt; about which we'll talk later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Side note&lt;/strong&gt;: For Typescript users the file is called as map.tsx.&lt;br&gt;
Inside the file put the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { MapContainer, TileLayer,Marker,Popup } from 'react-leaflet'
import 'leaflet/dist/leaflet.css'
import 'leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css'
import "leaflet-defaulticon-compatibility";

const Map = () =&amp;gt; {
  return (
    &amp;lt;MapContainer center={[40.8054,-74.0241]} zoom={14} scrollWheelZoom={false} style={{height: "100%", width: "100%"}}&amp;gt;
      &amp;lt;Marker 
      position={[40.8054,-74.0241]}
      draggable={true}
      animate={true}
      &amp;gt;
        &amp;lt;Popup&amp;gt;
          Hey ! you found me
        &amp;lt;/Popup&amp;gt;
      &amp;lt;/Marker&amp;gt;
    &amp;lt;/MapContainer&amp;gt;
  )
}

export default Map
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example I have used some basic attributes from react-leaflet to initialize the map.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;center&lt;/code&gt;: centers the map around the provided latitude &amp;amp; longitude.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;zoom&lt;/code&gt;: Initial zoom for the Map ranging from 0 to 18.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;scrollWheelZoom&lt;/code&gt;: yes it is exactly what it sounds like.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;position&lt;/code&gt;: sets the position for the Marker.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;draggable&lt;/code&gt;: helps drag and drop your marker on map.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;animate&lt;/code&gt;: if true, panning will always be animated.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are lot many features and examples available in &lt;a href="https://react-leaflet.js.org/docs/start-introduction" rel="noopener noreferrer"&gt;react-leaflet&lt;/a&gt; documentation.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Setting up Mapbox API&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In the above example, we will be using Mapbox API to add custom title layer to our map.&lt;br&gt;
Mapbox plugin is quietly supported by leaflet and it also provides you with many custom mapping styles, you can even create your very own styles in their studio, for this part of the tutorial will use the default styles.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flog8y0euh7du6r12u3sf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flog8y0euh7du6r12u3sf.jpg" alt="Alt Text" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first thing we’ll need to set up our custom Mapbox style is to have an account. I'm not going to walk you through that process, but you can head over to &lt;a href="https://www.mapbox.com/" rel="noopener noreferrer"&gt;Mapbox’s website&lt;/a&gt; where you can sign up for free.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To generate a token that we’ll use for providing access to our Map.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Head on over to the Account section of the Mapbox dashboard which you can access by clicking over your profile in the top right section of the navbar.&lt;/li&gt;
&lt;li&gt;Mapbox provides you with a “default” token that you can use in your applications. You're free to use this, but I recommend creating a new token that you can provide a unique name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Configuring our custom endpoint&lt;/strong&gt;&lt;br&gt;
For this tutorial, we’re going to use &lt;a href="https://docs.mapbox.com/api/maps/static-tiles/" rel="noopener noreferrer"&gt;Mapbox’s Static Tiles service&lt;/a&gt;. You can copy the endpoint from there which will look like this.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://api.mapbox.com/styles/v1/{username}/{style_id}/tiles/256/{z}/{x}/{y}@2x?access_token={access_token}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are a few parameters here we need to understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;username&lt;/code&gt;: this will be your Mapbox account’s username&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;style_id&lt;/code&gt;: this will be the ID of the style you are using&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;z, x, y&lt;/code&gt;: these are parameters that Leaflet programmatically swaps out, so we want to leave them as is&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;access_token&lt;/code&gt;: this is the Mapbox key you created above&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this part of the example, we are using styles provided by Mapbox itself. You can also create your very own styles in Mapbox but for now, will use the &lt;code&gt;streets-v11&lt;/code&gt; from &lt;a href="https://docs.mapbox.com/api/maps/styles/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And once I update my endpoint parameters, the final tilepoint URL will look like:&lt;br&gt;
&lt;code&gt;https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/256/{z}/{x}/{y}@2x?access_token=MY_ACCESS_TOKEN&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Since the style is provided by mapbox thus username in the URL is replaced with mapbox, if you are using your own style then you'll replace it with your own username.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Adding a custom TileLayer to React Leaflet&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Inside of your &lt;code&gt;&amp;lt;MapContainer&amp;gt;&lt;/code&gt; component in map.jsx you include a &lt;code&gt;&amp;lt;TileLayer&amp;gt;&lt;/code&gt; component, which defines the imagery of the world that you base your map upon.&lt;/p&gt;

&lt;p&gt;The example on the React Leaflet homepage uses a public version of &lt;a href="https://www.openstreetmap.org/#map=4/21.84/82.79" rel="noopener noreferrer"&gt;OpenStreetMap&lt;/a&gt; as their TileLayer, which is an open source map project created and updated by people all around the world.&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;MapContainer center={position} zoom={13}&amp;gt;
  &amp;lt;TileLayer
    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    attribution="&amp;amp;copy; &amp;lt;a href=&amp;amp;quot;http://osm.org/copyright&amp;amp;quot;&amp;gt;OpenStreetMap&amp;lt;/a&amp;gt; contributors"
  /&amp;gt;
&amp;lt;/MapContainer&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives you a basic map, but we want to swap in Mapbox so we can set up a custom look and feel for our map.&lt;/p&gt;

&lt;p&gt;To add our custom style, we’ll want to update the &lt;code&gt;url&lt;/code&gt; and &lt;code&gt;attribution&lt;/code&gt; props of the TileLayer component.&lt;/p&gt;

&lt;p&gt;For URL, it will simply be the custom style endpoint we created earlier, so in my example, it looks like:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/256/{z}/{x}/{y}@2x?access_token=MY_ACCESS_TOKEN&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For attribution, we want to credit Mapbox as the service, so we want to set our attribution as:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Map data &amp;amp;copy; &amp;lt;a href=&amp;amp;quot;https://www.openstreetmap.org/&amp;amp;quot;&amp;gt;OpenStreetMap&amp;lt;/a&amp;gt; contributors, &amp;lt;a href=&amp;amp;quot;https://creativecommons.org/licenses/by-sa/2.0/&amp;amp;quot;&amp;gt;CC-BY-SA&amp;lt;/a&amp;gt;, Imagery &amp;amp;copy; &amp;lt;a href=&amp;amp;quot;https://www.mapbox.com/&amp;amp;quot;&amp;gt;Mapbox&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When plugged in to our TileLayer, our map.jsx should now look 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;import { MapContainer, TileLayer, Marker, Popup } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css";
import "leaflet-defaulticon-compatibility";

const Map = () =&amp;gt; {
  return (
    &amp;lt;MapContainer
      center={[40.8054, -74.0241]}
      zoom={14}
      scrollWheelZoom={false}
      style={{ height: "100%", width: "100%" }}
    &amp;gt;
      &amp;lt;TileLayer
        url={`https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/256/{z}/{x}/{y}@2x?access_token=MY_ACCESS_TOKEN`}
        attribution='Map data &amp;amp;copy; &amp;lt;a href=&amp;amp;quot;https://www.openstreetmap.org/&amp;amp;quot;&amp;gt;OpenStreetMap&amp;lt;/a&amp;gt; contributors, &amp;lt;a href=&amp;amp;quot;https://creativecommons.org/licenses/by-sa/2.0/&amp;amp;quot;&amp;gt;CC-BY-SA&amp;lt;/a&amp;gt;, Imagery &amp;amp;copy; &amp;lt;a href=&amp;amp;quot;https://www.mapbox.com/&amp;amp;quot;&amp;gt;Mapbox&amp;lt;/a&amp;gt;'
      /&amp;gt;
      &amp;lt;Marker position={[40.8054, -74.0241]} draggable={true} animate={true}&amp;gt;
        &amp;lt;Popup&amp;gt;Hey ! I live here&amp;lt;/Popup&amp;gt;
      &amp;lt;/Marker&amp;gt;
    &amp;lt;/MapContainer&amp;gt;
  );
};

export default Map;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Finally let's render our Map&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As you might know, the global &lt;code&gt;window&lt;/code&gt; object is not available in SSR, you'll get a ReferenceError if you try using it there.&lt;br&gt;
Now to avoid this we can take advantage of Nextjs's dynamic loading which will help to prevent SSR.&lt;br&gt;
Inside &lt;code&gt;./pages/index.js&lt;/code&gt; emmbed your Map component 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;import React from "react";
import dynamic from "next/dynamic";

export default function Home() {
  const MapWithNoSSR = dynamic(() =&amp;gt; import("../component/map"), {
    ssr: false
  });

  return (
    &amp;lt;main&amp;gt;
      &amp;lt;div id="map"&amp;gt;
        &amp;lt;MapWithNoSSR /&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/main&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;And that's it you're good to go with something like this 👇&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/m7qnb"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Closing thoughts&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I hope this quick tutorial was helpful to you in some way 😊. I know it would have saved me quite a bit of work if I had this before I went down my Next.js + leafletjs path. Once you’ve got it all working, Don't forget to provide me with your valuable feedback. Good luck!👍&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>leafletjs</category>
      <category>react</category>
      <category>mapbox</category>
    </item>
  </channel>
</rss>
