next.js adsense 增加

8 min read

Add this to the head in _document.js

<script
            async
            src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
          ></script>

and use it like this (in my case i created a re-usable ad component)

import React, { useEffect } from "react";

const AdBanner = () => {
  useEffect(() => {
    try {
      (window.adsbygoogle = window.adsbygoogle || []).push({});
    } catch (err) {
      console.log(err);
    }
  }, []);

  return (
    <ins
      className="adsbygoogle adbanner-customize"
      style={{
        display: "block"
      }}
      data-ad-client=<your-client-id>
      data-ad-slot=<slot-id>
    />
  );
};

export default AdBanner;