Skip to content

Data Island Integration: Embed RDF in HTML with solidos-lite #7

Description

@melvincarvalho

Summary

Integrate solidos-lite to embed RDF data directly in HTML responses, eliminating the need for a second HTTP request when viewing resources in the browser.

Current Behavior

Browser → GET /resource (Accept: text/html)
Server  → Returns HTML wrapper that loads mashlib from CDN
Browser → mashlib initializes, fetches /resource AGAIN (Accept: text/turtle)
Server  → Returns RDF data
Browser → mashlib renders the data

Problems:

  • Two HTTP round-trips required
  • Cannot work offline after initial load
  • Not suitable for static HTML exports
  • Latency penalty for every resource view

Proposed Behavior

Browser → GET /resource (Accept: text/html)
Server  → Returns HTML with:
          - Embedded RDF in <script type="text/turtle"> data island
          - solidos-lite shim that intercepts mashlib fetcher
Browser → solidos-lite patches fetcher.load()
Browser → mashlib reads from data island (no network request)
Browser → Renders immediately

How solidos-lite Works

solidos-lite intercepts mashlib RDF fetching by patching fetcher.load:

const originalLoad = fetcher.load.bind(fetcher)

fetcher.load = async function(uri, options = {}) {
  const island = findDataIsland(uriStr)
  
  if (island) {
    // Parse locally embedded data instead of fetching
    $rdf.parse(content, store, baseUri, contentType)
    fetcher.requested[baseUri] = "done"
    return doc
  }
  
  // Fall back to original network fetch
  return originalLoad(uri, options)
}

Key interception points:

  1. fetcher.load() - main fetch function
  2. fetcher.nowOrWhenFetched() - callback-based fetching
  3. fetcher.getState() - reports "fetched" for data island URIs

Implementation Plan

Phase 1: Basic Integration

File: src/mashlib/index.js

Update generateDatabrowserHtml() to:

  1. Accept RDF content as parameter
  2. Embed content in <script type="text/turtle" data-uri="..."> tag
  3. Load solidos-lite from CDN before mashlib

Phase 2: Handler Integration

File: src/handlers/resource.js

Pass RDF content to HTML generator when serving mashlib view.

Phase 3: Content Type Handling

  • text/turtle → embed directly
  • application/ld+json → embed as JSON-LD
  • text/n3 → embed directly
  • application/rdf+xml → fallback to fetch

Phase 4: Large Resource Handling

const MAX_EMBED_SIZE = 100 * 1024; // 100KB threshold

if (rdfContent.length > MAX_EMBED_SIZE) {
  return generateDatabrowserHtml(resourceUrl); // fetch-based
} else {
  return generateDatabrowserHtml(resourceUrl, rdfContent, contentType); // embedded
}

Benefits

Benefit Description
Performance Single HTTP request instead of two
Offline Support Save HTML file, view offline
Static Export Generate static HTML sites from Solid data
Reduced Server Load Half the requests for browser views
Better UX Faster initial render, no loading flicker

Considerations

Security

  • Embedded content must be properly escaped to prevent XSS
  • RDF in <script> tags is generally safe (not executed as JS)

Caching

  • Browser caching differs for embedded vs fetched content
  • Consider ETags/Last-Modified for HTML response

Linked Resources

  • solidos-lite falls back to network fetch for linked resources
  • Only primary resource is embedded

Testing Plan

  1. Unit Tests: generateDatabrowserHtml() with embedded content
  2. Integration Tests: Verify single network request in devtools
  3. Manual Tests: Save HTML, open locally, verify offline rendering

References

Related Issues


LLM-friendly issue for future implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions