Google Docs viewer returning 204 responses, no longer working, alternatives?

后端 未结 7 1908
执念已碎
执念已碎 2020-12-08 01:01

UPDATE 2016-11-16 9:53am EST

It appears many people are still seeing 204 responses even though Google has claimed to have \"fixed\" the problem. Wh

相关标签:
7条回答
  • 2020-12-08 01:58

    Here is a react example in functional component

    import React, {useEffect, useRef, useState} from "react";
    
    type IframeGoogleDocsProps = {
        url: string,
        className?: string
    };
    export default function IframeGoogleDoc({url, className}: IframeGoogleDocsProps) {
    
        const [iframeTimeoutId, setIframeTimeoutId] = useState(undefined);
        const iframeRef = useRef(null);
    
        useEffect(()=>{
            const intervalId = setInterval(
                updateIframeSrc, 1000 * 3
            );
            setIframeTimeoutId(intervalId)
        },[]);
    
        function iframeLoaded() {
            clearInterval(iframeTimeoutId);
        }
        function getIframeLink() {
            return `https://docs.google.com/gview?url=${url}&embedded=true`;
        }
        function updateIframeSrc() {
            if(iframeRef.current){
                iframeRef.current.src = getIframeLink();
            }
        }
    
        return (
            <iframe
                className={className}
                onLoad={iframeLoaded}
                onError={updateIframeSrc}
                ref={iframeRef}
                src={getIframeLink()}
            />
        );
    }
    
    0 讨论(0)
提交回复
热议问题