Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage Api breaks with Server-Side Rendering (Angular Universal). #135

Open
ribalnasr opened this issue Jul 20, 2020 · 3 comments
Open

Comments

@ribalnasr
Copy link

ribalnasr commented Jul 20, 2020

When using the SDK with Angular Universal, the SSR version (which is rendered server-side) gives the following error:

The Firebase client-side SDK cannot access the Storage Bucket server-side.
Please use the admin SDK instead - https://www.npmjs.com/package/firebase-admin

Which is usually understandable but not applicable in this particular case.

This error only shows if I'm using the storage service or when populating fields of type Media.

For a while now I've been using isPlatformBrowser() before populating media fields since I didn't need the media URLs for SEO purposes before, however now, since am integrating Twitter and Facebook's open graph meta tags, I need these URLs to be included in the source-code.

How do you see we should proceed here? Should you fix that from your side or is it something I can do as a workaround from my side to make sure the URLs are retrieved on server-side too?

@gitdubz
Copy link
Contributor

gitdubz commented Jul 23, 2020

@ribalnasr is this resolved by adding the cert as mentioned here?
#136

@ribalnasr
Copy link
Author

hi, no, this is not really related, the other issue is when using flamelink on server-side, but in this case here, flamelink is used on client-side using the firebase sdk (not firebase-admin), however since we're using angular universal, we compile the site to be served as a firebase function instead of uploading it to firebase hosting, and we redirect the hosting to the function. as you probably know this is done in order to make the source code seo-friendly, but since this process runs the website on server-side first then on client-side, the firebase client-side sdk (used by flamelink sdk in the website) shows the above error in the functions logs (not in the browser's console).

I hope this makes it clearer, otherwise i will need to create a full example on how to reproduce this later since i can't publicly share the current projects am working on.

@ribalnasr
Copy link
Author

since there is no activity on this issue here's the workaround i did:

i created a firebase function that expects the image id and returns the image as follows:

important: you need to initialize the firebase app using the service account certificate and not the default admin.initializeApp() for this to work

import * as functions from 'firebase-functions';
import flamelink from './flamelink'; // this is where i initialized the flamelink app
import axios from 'axios';

export const metaImage = functions.https.onRequest(async (request, response) => {
    const fileId = request.path.split('/')[1];

    if (!fileId) {
        throw 'Image ID is not specified.';
    }

    const url = await flamelink.storage.getURL({
        fileId,
        size: { width: '1440' }
    });

    const image = await axios.get(url, {
        responseType: 'arraybuffer'
    })

    response.contentType('jpg').send(image.data);

});

then i add the required meta tags using the function link. ex:

    this.meta.addTag({
        name: 'og:image',
        content: 'https://us-central1-<PROJECT_NAME>.cloudfunctions.net/metaImage/<IMAGE_ID>' 
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants