Memuat Javascript Biasa Svelte Astro
2 mins read

Memuat Javascript Biasa Svelte Astro

mrfdn.com – Jika kita mempunyai file javascript yang berjalan normal tanpa menggunakan framework seperti Svelte yang ingin kita jalankan pada proyek Astro, biasanya kita mendapatkan error:

document is not defined

Browser APIs are not available on the server.

If the code is in a framework component, try to access these objects after rendering using lifecycle methods or use a client:only directive to make the component exclusively run on the client.

See  for more information.

Jadi untuk menjalankan artikel kita perlu memasukkannya ke dalam pekerjaan onMount() yang secara default untuk Svelte.

Kode sumbernya terlihat seperti ini:

import { onMount } from 'svelte';

onMount(() => {
  // Paste plain javascript di sini.
  // Code to run when the component is mounted
});

Fungsi onMount menjalankan skrip di dalamnya ketika sebuah komponen dipanggil.

Contoh penggunaan Svelte onMount di proyek Astro#

  1. Buat templat komponen yang ramping MyComponent.svelte (pastikan nama bagian dimulai dengan huruf kapital) lalu pastekan kode ini di dalamnya,
<script>
import { onMount } from 'svelte';

onMount(() => {
function say() {
  const main = document.querySelector("#say");
  main.innerHTML = "<p>Halo</p>";
  };
  say();
});

</script>

<div id="say"></div>
  1. Di bagian lain, panggil Astro dan Svelte dengan membuat div dengan id #say. Berpura-pura melakukan sesuatu dengan nama itu OtherComponent.astrolalu tambahkan penyangga client:load di tag sehingga MyComponent dapat dimuat secara otomatis.
---
import MyComponent from "./MyComponent.svelte";
---

<MyComponet client:load/>
  1. Layar akan menampilkan teks Halo.

Ini sudah berakhir.

Detail lebih lanjut tentang onMount.

Semoga bermanfaat.

PakarPBN

A Private Blog Network (PBN) is a collection of websites that are controlled by a single individual or organization and used primarily to build backlinks to a “money site” in order to influence its ranking in search engines such as Google. The core idea behind a PBN is based on the importance of backlinks in Google’s ranking algorithm. Since Google views backlinks as signals of authority and trust, some website owners attempt to artificially create these signals through a controlled network of sites.

In a typical PBN setup, the owner acquires expired or aged domains that already have existing authority, backlinks, and history. These domains are rebuilt with new content and hosted separately, often using different IP addresses, hosting providers, themes, and ownership details to make them appear unrelated. Within the content published on these sites, links are strategically placed that point to the main website the owner wants to rank higher. By doing this, the owner attempts to pass link equity (also known as “link juice”) from the PBN sites to the target website.

The purpose of a PBN is to give the impression that the target website is naturally earning links from multiple independent sources. If done effectively, this can temporarily improve keyword rankings, increase organic visibility, and drive more traffic from search results.

Jasa Backlink

Download Anime Batch