Skip to content

Svelte <SlopText /> ​

The @slopmachine/svelte package provides a top-tier Developer Experience (DX) for seamlessly rendering Slop Machine generated text (markdown) in your Svelte applications.

By default, the component handles loading states with a nice built-in shimmer effect and loading spinner. It automatically fetches the markdown content from the generated text URL, parses it, and renders it cleanly once it is ready.

Try the Live Interactive Svelte Demo to see the component in action!

Installation ​

bash
npm install @slopmachine/svelte

Basic Usage ​

The minimum required prop is bucketId. This ties the component to a specific AI generation task.

svelte
<script>
  import { SlopText } from "@slopmachine/svelte";
</script>

<div class="prose max-w-none">
  <SlopText bucketId="my-unique-bucket-id" />
</div>

Advanced Usage ​

You can override variables, pass custom metadata at runtime, pick a model, and override the default loading skeleton and error state via the fallback and errorFallback snippets. SlopText automatically fetches the text from the renderText endpoint and renders it as Markdown.

The generated Markdown is converted to HTML and sanitized with DOMPurify before rendering, so scripts, event handlers, and javascript: links in model output are stripped. During server-side rendering, where no DOM is available, the text is rendered HTML-escaped instead.

svelte
<script>
  import { SlopText } from "@slopmachine/svelte";
</script>

<SlopText
  bucketId="blog-post-bucket"
  model="gemini-pro"
  variables={{ topic: "cyberpunk", length: "long" }}
  metadata={{ authorId: "usr_42", department: "content" }}
  class="text-lg text-gray-800"
>
  {#snippet fallback()}
    <div class="flex h-32 items-center justify-center text-blue-500">
      Generating post content...
    </div>
  {/snippet}
  {#snippet errorFallback({ error })}
    <div>Failed to load post: {error.message}</div>
  {/snippet}
</SlopText>

Preloading ​

You can import and use preloadText from @slopmachine/svelte to cache the asset before rendering the component.

svelte
<script>
  import { preloadText } from "@slopmachine/svelte";

  // Call this early in your component lifecycle or route load function
  preloadText({ bucketId: "my-unique-bucket-id" });
</script>

Props Reference ​

bucketId ​

Type: string (Required) The unique identifier for the specific text generation session/bucket.

model ​

Type: string (Optional, e.g. "gemini-pro") Overrides the AI model used for generation. Defaults to the bucket version's configured model. Ignored if resultId is provided.

version ​

Type: number (Optional) Useful for cache-busting or fetching a specific generation iteration.

resultId ​

Type: string (Optional) If a specific result ID is known, it can be fetched directly.

variables ​

Type: Record<string, string | number | undefined | null> (Optional) A dictionary of prompt variables interpolated dynamically. Example: { topic: "svelte", style: "tutorial" }. Any extraneous or unused variables provided that are not required by the resolved templates are automatically stripped out to ensure they do not unnecessarily bust the cache.

metadata ​

Type: Record<string, any> (Optional) Arbitrary custom metadata to attach to the generation request and resulting document. Available for both Buckets and Pipelines. Example: { userId: "usr_123", authorId: "usr_42" }.

attachments ​

Type: string[] (Optional) An array of string URLs representing temporary file attachments to be used by the generative AI model.

baseUrl ​

Type: string (Optional) Full endpoint URL to send requests to instead of the default production renderText endpoint (or renderPipeline when using pipelineId).

fallback (Snippet) ​

Replaces the default spinner and shimmer effect. Pass a snippet to render a custom skeleton or text while the content is loading.

errorFallback (Snippet) ​

Rendered instead of the text if generation or fetching fails. Receives { error }, a SlopMachineError. If not provided, a default "Failed to load text" message is shown.

onGenerationError ​

Type: (error: SlopMachineError) => void (Optional) Called once per URL when the text fails to generate or load. error.status holds the HTTP status (e.g. 400 for a missing variable) when the API reported the failure, and error.message holds the API's error message.

HTML <div> Attributes ​

You can pass standard attributes like class, style, etc. These props will be applied to the outer wrapper div that contains the rendered Markdown content. Note that in Svelte, you use class instead of React's className.