#CORS - protocols must match

5 messages · Page 1 of 1 (latest)

wide bay
#

I'm trying to call a function inside an iframe from the app window. Iframes render fine but webview obviously moans about protocols not matching. How can I solve/bypass this restriction?

Blocked a frame with origin "tauri://localhost" from accessing a frame with origin "asset://localhost". The frame requesting access has a protocol of "tauri", the frame being accessed has a protocol of "asset". Protocols must match.

wide bay
#
  • I've tried using http to fetch the document contents (using both asset:// and file://)

=> URL scheme is not allowed

wide bay
#

ok for future visitors; I've solved this

#

I'm using react but you can pretty much apply the same logic to any fe tool

#

create an iframe component and pass the iframe src (in my usecase it is a local html doc);

import React, { useRef, useEffect, useState } from 'react';
import { readTextFile, BaseDirectory } from '@tauri-apps/api/fs';

export default function CustomIframe(props) {
  const [htmlString, setHtmlString] = useState('');
  const [isLoading, setLoading] = useState(true);
  const iframeRef = useRef(null);

  useEffect(() => {
    const fetchData = async () => {
      try {
        const result = await readTextFile(props.src, { dir: BaseDirectory.AppData });
        setHtmlString(result);
        setLoading(false);
      } catch (error) {
        setLoading(false);
        props.onError();
      }
    };

    fetchData();
  }, [props.src]);

  useEffect(() => {
    if (iframeRef.current) {
      const iframeDocument = iframeRef.current.contentDocument;
      iframeDocument.open();
      iframeDocument.write(htmlString);
      iframeDocument.close();
    }
  }, [htmlString]);

  if (isLoading) {
    return '';
  } else {
    return (
      <iframe
        id={`frame-${props.id}`}
        className='advanced-slide'
        title={`Advanced Slide ${props.id}`}
        ref={iframeRef}
        sandbox="allow-scripts allow-same-origin"
        src={`data:text/html;charset=utf-8,${encodeURIComponent(htmlString)}`}
      />
    );
  }
}